-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemote_Control.ino
More file actions
316 lines (262 loc) · 8.71 KB
/
Remote_Control.ino
File metadata and controls
316 lines (262 loc) · 8.71 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_MODULE
#define stepsPerRevolution 2048
#include <Dabble.h>
#include <math.h>
#include <Stepper.h>
#include <Servo.h>
#include <AFMotor.h>
// motor 1 = left motor
// motor 2 = right motor
// thwacker motor
Stepper thwack_motor(stepsPerRevolution, 36, 38, 40, 42);
// LEDs array
int LED_array[] = {23, 25, 27, 29, 31, 33, 35, 37, 39};
// gun motor variables
Servo gun_motor;
int servo_position = 30;
// Motor controller pins:
int L_EN1 = 18; // ch1 motor1
int L_EN2 = 19; // ch2 motor1
int R_EN1 = 20; // ch1 motor2
int R_EN2 = 21; // ch2 motor2
int relay_pin = 30;
// pulse durations
unsigned long lastPulseL, lastPulseR;
// encoder tick counter variables
unsigned long num_ticksL = 0;
unsigned long num_ticksR = 0;
// width of pulse in ms we want the motor to run
int motorTime = 1000;
// state of the thwacker
int is_thwacker_down = false;
int is_relay_conn = false;
int is_LEDs_on = false;
// something
float motorTime_L, motorTime_R;
// encoder tick timing variables
volatile float ltime = 0.0;
volatile float rtime = 0.0;
volatile float time_elapsedR = 0.0;
volatile float time_elapsedL = 0.0;
// Motor speed
int desired_speed = 200;
bool motor_dirL = 0;
bool motor_dirR = 0;
byte motor_speedL = desired_speed;
byte motor_speedR = desired_speed;
#define runEvery(t) for (static typeof(t) _lasttime;(typeof(t))((typeof(t))millis() - _lasttime) > (t);_lasttime += (t))
// motor will be on motor shield 3 and 4
AF_DCMotor L_motor(3);
AF_DCMotor R_motor(4);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // make sure your Serial Monitor is also set at this baud rate.
Dabble.begin(9600); //Enter baudrate of your bluetooth.Connect bluetooth on Bluetooth port present on evive.
pinMode(L_EN1, INPUT);
pinMode(L_EN1, INPUT);
pinMode(R_EN2, INPUT);
pinMode(R_EN2, INPUT);
pinMode(relay_pin, OUTPUT);
L_motor.setSpeed(motor_speedL);
R_motor.setSpeed(motor_speedR);
attachInterrupt(L_EN1, count_tickL, CHANGE);
attachInterrupt(L_EN2, check_speedL, CHANGE);
attachInterrupt(R_EN1, count_tickR, CHANGE);
attachInterrupt(R_EN2, check_speedR, CHANGE);
// thwack stepper motor init
thwack_motor.setSpeed(25);
// relay init
digitalWrite(relay_pin, HIGH);
for (int i = 0; i < 9; i++) {
pinMode(LED_array[i], OUTPUT);
}
// gun servo motor init
gun_motor.attach(10);
gun_motor.write(servo_position);
}
void loop() {
Dabble.processInput(); //this function is used to refresh data obtained from smartphone.Hence calling this function is mandatory in order to get data properly from your mobile.
if (GamePad.isCrossPressed()) {
shoot();
}
if (GamePad.isTrianglePressed()) {
if (!is_LEDs_on) {
for (int i = 0; i < 9; i++) {
digitalWrite(LED_array[i], HIGH);
}
} else {
for (int i = 0; i < 9; i++) {
digitalWrite(LED_array[i], LOW);
}
}
is_LEDs_on = !is_LEDs_on;
}
if (GamePad.isSquarePressed()) {
if (is_thwacker_down) {
thwacker_raise_up();
} else {
thwacker_pull_down();
}
is_thwacker_down = !is_thwacker_down;
}
if (GamePad.isCirclePressed()) {
if (is_relay_conn) {
digitalWrite(relay_pin, HIGH);
}
else {
digitalWrite(relay_pin, LOW);
}
is_relay_conn = !is_relay_conn;
}
// https://www.impulseadventure.com/elec/robot-differential-steering.html
// INPUTS
int joy_x = GamePad.getXaxisData(); // Joystick X input (-128..+127)
int joy_y = GamePad.getYaxisData(); // Joystick Y input (-128..+127)
/* // OUTPUTS
int nMotMixL; // Motor (left) mixed output (-128..+127)
int nMotMixR; // Motor (right) mixed output (-128..+127)
// CONFIG
// - fPivYLimt : The threshold at which the pivot action starts
// This threshold is measured in units on the Y-axis
// away from the X-axis (Y=0). A greater value will assign
// the joystick's range to pivot actions.
// Allowable range: (0..+127)
float fPivYLimit = 32.0;
// TEMP VARIABLES
float nMotPremixL; // Motor (left) premixed output (-128..+127)
float nMotPremixR; // Motor (right) premixed output (-128..+127)
int nPivSpeed; // Pivot Speed (-128..+127)
float fPivScale; // Balance scale b/w drive and pivot ( 0..1 )
// Calculate Drive Turn output due to Joystick X input
if (nJoyY >= 0) {
// Forward
nMotPremixL = (nJoyX>=0)? 127.0 : (127.0 + nJoyX);
nMotPremixR = (nJoyX>=0)? (127.0 - nJoyX) : 127.0;
} else {
// Reverse
nMotPremixL = (nJoyX>=0)? (127.0 - nJoyX) : 127.0;
nMotPremixR = (nJoyX>=0)? 127.0 : (127.0 + nJoyX);
}
// Scale Drive output due to Joystick Y input (throttle)
nMotPremixL = nMotPremixL * nJoyY/128.0;
nMotPremixR = nMotPremixR * nJoyY/128.0;
// Now calculate pivot amount
// - Strength of pivot (nPivSpeed) based on Joystick X input
// - Blending of pivot vs drive (fPivScale) based on Joystick Y input
nPivSpeed = nJoyX;
fPivScale = (abs(nJoyY)>fPivYLimit)? 0.0 : (1.0 - abs(nJoyY)/fPivYLimit);
// Calculate final mix of Drive and Pivot
nMotMixL = (1.0-fPivScale)*nMotPremixL + fPivScale*( nPivSpeed);
nMotMixR = (1.0-fPivScale)*nMotPremixR + fPivScale*(-nPivSpeed);
*/
//if (((int)nMotMixL) > -128) {
int motor_speedLeft = (cos(45)*(-joy_x) - sin(45)*joy_y) * (255.0/7.0);
//}
//else {
//motor_speedL = 254;
//}
//if (((int)nMotMixR) > -128) {
int motor_speedRight = (sin(45)*(-joy_x) + cos(45)*joy_y) * (255.0/7.0);
//}
//else {
//motor_speedR = 254;
//}
motor_dirL = motor_speedLeft<0;
motor_dirR = motor_speedRight>0;
motorTime_L = motorTime*(256.0/motor_speedL);
motorTime_R = motorTime*(256.0/motor_speedR);
/*runEvery(5) {
lastPulseL = millis() - ltime; // Check how much time has passed since last pulse
lastPulseR = millis() - rtime;
if (lastPulseL > motorTime && motor_speedL < desired_speed) // If the pulse is longer than the time we want speed up motor
{
motor_speedL = motor_speedL + 5;
}
if (time_elapsedL < motorTime && motor_speedL > 0) // If the pulse is shorter than the time we want slow down motor
{
motor_speedL = motor_speedL - 5;
}
if (lastPulseR > motorTime && motor_speedR < desired_speed) {
motor_speedR = motor_speedR + 5;
}
if (time_elapsedR < motorTime && motor_speedR > 0) {
motor_speedR = motor_speedR - 5;
}
//if (num_ticksL < num_ticksR) {
// not enough ticks on the left motor, so increase its speed
//motor_speedL = motor_speedL + 5;
//} else if (num_ticksL > num_ticksR) {
// not enough ticks on the right motor, increase right motor speed
//motor_speedR = motor_speedR + 5;
//}
L_motor.setSpeed(motor_speedLeft);
R_motor.setSpeed(motor_speedRight);
}
*/
Serial.print("Left Motor Speed: ");
//if (motor_dirL) {
//Serial.print("-");
//}
Serial.println(motor_speedLeft);
Serial.print("Right Motor Speed: ");
//if (motor_dirR) {
//Serial.print("-");
//}
Serial.println(motor_speedRight);
Serial.println();
/*Serial.println(GamePad.getXaxisData());
Serial.println(GamePad.getYaxisData());
Serial.println();
*/
L_motor.setSpeed(abs(motor_speedLeft));
R_motor.setSpeed(abs(motor_speedRight));
// forward = 1
// backwards = 0
if (motor_dirL == 1) {
L_motor.run(FORWARD);
} else {
L_motor.run(BACKWARD);
}
if (motor_dirR == 1) {
R_motor.run(FORWARD);
} else {
R_motor.run(BACKWARD);
}
//delay(5);
}
void count_tickL() {
num_ticksL++;
}
void count_tickR() {
num_ticksR++;
}
void check_speedL() {
time_elapsedL = millis() - ltime;
ltime = millis();
}
void check_speedR() {
time_elapsedR = millis() - rtime;
rtime = millis();
}
void shoot() {
gun_motor.write(servo_position + 45);
delay(100);
gun_motor.write(servo_position);
}
void thwacker_raise_up() {
thwack_motor.setSpeed(25);
thwack_motor.step(7000);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
}
void thwacker_pull_down() {
thwack_motor.setSpeed(25);
thwack_motor.step(-7000);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
}