-
Notifications
You must be signed in to change notification settings - Fork 0
/
square.cpp
125 lines (92 loc) · 2.99 KB
/
square.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/joystick.h>
#include <stdlib.h>
#include <algorithm>
#include <wiringPi.h>
#include <softPwm.h>
#include <pthread.h>
#include <stdint.h>
#include <math.h>
using namespace std;
// how to compile: gcc square.cpp -o square -lwiringPi -lpthread
#define NORM_SPEED 1.0
#define SLOW_SPEED 0.3
#define FAST_SPEED 0.5
// Motor layout as follows: direction pin, speed pin, reverser
static int motorPins[4][3] = {{0, 4, 1}, {2, 5, 1}};
static int motorCount = 2;
//Pink zip tie is front, Purple zip tie is back
enum motor {
motorL = 0,
motorR = 1,
};
//Stores commanded mecanum movement vector
struct mecanumVector {
double x, y, h;
};
//Keeps value between minimum and maximum
double constrain(double value, double min, double max) {
if(value < min)
return min;
if(value > max)
return max;
return value;
}
struct mecanumVector mVec, position;
//Sets power to motor using dir pin and pwm pin
void setPower(int motorNum, double power) {
int absPower = (int)abs(power * 80.0);
int direct = (((float)motorPins[motorNum][2] * power) > 0) ? 1 : 0;
if(absPower == 0) {
absPower = 1;
}
//printf("motor: %.1d direct: %.1d power: %d\n", motorNum, direct, absPower);
digitalWrite(motorPins[motorNum][0], direct);
//analogWrite(motorPins[motorNum][1], absPower);
softPwmWrite(motorPins[motorNum][1], absPower);
}
//Converts forward, strafe, and turn commands into motor powers then sets powers to motors
void drive(double forward, double strafe, double turn) {
double powerL = forward - turn;
double powerR = forward + turn;
double powerMax = max(abs(powerL), abs(powerR));
if (powerMax > 1.0) {
powerL /= powerMax;
powerR /= powerMax;
}
powerL = constrain(powerL, -1.0, 1.0);
powerR = constrain(powerR, -1.0, 1.0);
//printf("\n\n\nfl: %.2f fr: %.2f\n\nbl: %.2f br: %.2f\n", powerFL, powerFR, powerBL, powerBR);
setPower(motorL, powerL);
setPower(motorR, powerR);
}
void *printPeriodically(void * ignore) {
while(true) {
delay(500);
}
}
int main(int argc, char *argv[]) {
pthread_t printTID;
printf("in code first");
wiringPiSetup();
// Sets up pinouts for drivebase
for(int i = 0; i < motorCount; i++) {
//printf("Creating software PWM #%.1d on pin %.1d\n", i, motorPins[i][1]);
softPwmCreate(motorPins[i][1], 0, 100);
pinMode(motorPins[i][0], OUTPUT);
//pinMode(motorPins[i][1], OUTPUT);
}
// Thread for printing things without having to be in the loop where they print so fast they are cut off
pthread_create(&printTID, NULL, printPeriodically, (void * )NULL);
drive(0.5, 0.0, 0.0);
sleep(50);
drive(0.0,0.0,0.0);
sleep(1000);
drive(0.0, 0.0, 1.0);
sleep(50);
drive(0.5,0.0, 0.0);
sleep(100);
drive(0.0,0.0,0.0);
}