-
Notifications
You must be signed in to change notification settings - Fork 1
/
differential_drive.ino
173 lines (138 loc) · 4.48 KB
/
differential_drive.ino
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <PS3BT.h>
#include <usbhub.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
namespace robot {
namespace actuator {
class DCMotor {
public:
DCMotor(const uint8_t pwm_enabled_pin,
const uint8_t forward_pin,
const uint8_t backward_pin);
void SetVelocity(const int v);
private:
uint8_t pwm_pin;
uint8_t fwd_pin;
uint8_t bwd_pin;
};
DCMotor::DCMotor(const uint8_t pwm_enabled_pin,
const uint8_t forward_pin,
const uint8_t backward_pin) {
pwm_pin = pwm_enabled_pin;
fwd_pin = forward_pin;
bwd_pin = backward_pin;
pinMode(pwm_pin, OUTPUT);
pinMode(fwd_pin, OUTPUT);
pinMode(bwd_pin, OUTPUT);
}
// Input must be between -255 and 255, use map() and constrain()
void DCMotor::SetVelocity(int v) {
if (v < -255 || v > 255) {
Serial.print(F("\nVelocity must be between -255 and 255, inclusive: "));
Serial.print(v);
v = constrain(v, -255, 255);
}
// Account for motor deadband regions where
// motor will not move from speeds of 0 to 80
if (v > -80 && v < -5) v = -80;
else if (v < 80 && v > 5) v = 80;
// Write speed to PWM pin
analogWrite(pwm_pin, abs(v));
// Set direction to be either forward or backward, never both
if (v > 0) {
digitalWrite(fwd_pin, HIGH);
digitalWrite(bwd_pin, LOW);
} else {
digitalWrite(bwd_pin, HIGH);
digitalWrite(fwd_pin, LOW);
}
}
} // namespace robot
} // namespace actuator
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
PS3BT PS3(&Btd); // This will just create the instance
const int IN1 = 7;
const int IN2 = 6;
const int IN3 = 5;
const int IN4 = 4;
const int ENA = 9;
const int ENB = 3;
robot::actuator::DCMotor left_motor (ENB, IN3, IN4);
robot::actuator::DCMotor right_motor (ENA, IN1, IN2);
bool printAngle;
// Responsiveness of steering instead of straight motion
//const double STEER = 0.6;
const double MAX_SPEED = 255;
void setup() {
Serial.begin(115200);
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}
void loop() {
Usb.Task();
if (!PS3.PS3Connected) {
return;
}
if (PS3.getButtonClick(TRIANGLE)) {
Serial.print(F("\r\nTriangle"));
PS3.setRumbleOn(RumbleLow);
}
if (PS3.getButtonClick(CIRCLE)) {
Serial.print(F("\r\nCircle"));
PS3.setRumbleOn(RumbleHigh);
}
// Left trigger
Serial.print("\n");
Serial.print(PS3.getAnalogButton(L2) / 255.0);
float STEER = PS3.getAnalogButton(L2) / 255.0;
int left_motor_v = 0;
int right_motor_v = 0;
// Joystick is 0 (top edge) to 255 (bottom edge)
int joystick_vert = map(PS3.getAnalogHat(RightHatY),
0, 255,
(2-STEER) * floor(MAX_SPEED/2), -(2-STEER) * floor(MAX_SPEED/2));
// Joystick is 0 (left edge) to 255 (right edge)
int joystick_horiz = map(PS3.getAnalogHat(RightHatX),
0, 255,
-STEER * floor(MAX_SPEED/2), STEER * floor(MAX_SPEED/2));
// Only move when outside of joystick deadband, which is
// + or - 5% around resting position
if (joystick_vert < floor(-0.05 * MAX_SPEED/2) ||
joystick_vert > ceil(0.05 * MAX_SPEED/2)) {
//Serial.print(F("\tRightHatY: "));
//Serial.print(PS3.getAnalogHat(RightHatY));
right_motor_v = left_motor_v = joystick_vert;
}
// Only move when outside of joystick deadband, which is
// + or - 5% around resting position
if (joystick_horiz < floor(-0.05 * MAX_SPEED/2) ||
joystick_horiz > ceil(0.05 * MAX_SPEED/2)) {
//Serial.print(F("\tRightHatX: "));
//Serial.print(PS3.getAnalogHat(RightHatX));
if (joystick_vert < -80) {
left_motor_v -= joystick_horiz;
right_motor_v += joystick_horiz;
} else {
left_motor_v += joystick_horiz;
right_motor_v -= joystick_horiz;
}
}
if (left_motor_v != 0 && right_motor_v != 0) {
//Serial.print(F("\nMotor Velocities (L, R): "));
//Serial.print(left_motor_v);
//Serial.print(F(", "));
//Serial.print(right_motor_v);
}
left_motor.SetVelocity(constrain(left_motor_v, -MAX_SPEED, MAX_SPEED));
right_motor.SetVelocity(constrain(right_motor_v, -MAX_SPEED, MAX_SPEED));
}