Repository navigation
Expand file tree
/
Copy pathMotorizedBarriers.c
More file actions
387 lines (335 loc) · 8.17 KB
/
Copy pathMotorizedBarriers.c
File metadata and controls
387 lines (335 loc) · 8.17 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
GOALMAZE PERIPHERAL MOTOR AND LIGHT SYSTEM
------------------------------------------
This file contains functionality for
(A) Raising/lowering maze barriers
(B) Lights
1. Dimming
2. Color change
*/
// include the SPI library:
#include <SPI.h>
/////////////////////////////////
// MOTOR Settings
/////////////////////////////////
// L9958 slave select pins for SPI
#define SS_M4 14
#define SS_M3 13
#define SS_M2 12
#define SS_M1 11
int mSS[4] = {SS_M1, SS_M2, SS_M3, SS_M4};
// L9958 DIRection pins
#define DIR_M1 2
#define DIR_M2 3
#define DIR_M3 4
#define DIR_M4 7
int mDIR[4] = {DIR_M1, DIR_M2, DIR_M3, DIR_M4};
// L9958 PWM pins
#define PWM_M1 9
#define PWM_M2 10 // Timer1
#define PWM_M3 5
#define PWM_M4 6 // Timer0
int mPWM[4] = {PWM_M1, PWM_M2, PWM_M3, PWM_M4};
// Pins used to command the barrier to flip
int controlPins[] = {30,31,32,33};
// Shortcut functions for up and down
#define UP 1
#define DOWN 0
// L9958 Enable for all 4 motors
#define ENABLE_MOTORS 8
int pwm1, pwm2, pwm3, pwm4;
boolean dir1, dir2, dir3, dir4;
// Motor Functions
void motorsOff();
void motorMove(int motor, int dir, int speed);
void motorFlip(int motor);
void generalPattern();
void initializeMotor(int motor, unsigned int configWord);
int motorSpeed(int motor, int direction);
// Digital potentiometer settings
//////////////////////////////////
byte address = 0x00;
int CS= 10;
// Digital motor state trackers
int motorStates[] = {DOWN, DOWN, DOWN, DOWN};
// Whether or not to execute movements in parallel or series
boolean parallel = true;
/* SETUP */
/////////////////////////////////
void setup() {
/*
PART 1: Motor pin mode setup!
*/
unsigned int configWord;
// put your setup code here, to run once:
for (int i = 0; i < 4; i++)
{
pinMode(mSS[0], OUTPUT);
digitalWrite(mSS[0], LOW); // HIGH = not selected
}
// L9958 DIRection pins: Q...how are direction pins different from PWM?
for (int i = 0; i < 4; i++)
pinMode(mDIR[i], OUTPUT);
// L9958 PWM pins: Q...how are PWM different from direction?
for (int i = 0; i<4; i++)
{
pinMode(mPWM[0], OUTPUT);
digitalWrite(mPWM[0], LOW);
}
// L9958 Enable for all 4 motors
pinMode(ENABLE_MOTORS, OUTPUT);
motorOff(true);
/*
PART 2: Transfer configurations to each motor
*/
configWord = 0b0000010000001100;
SPI.begin();
SPI.setBitOrder(LSBFIRST);
SPI.setDataMode(SPI_MODE1); // clock pol = low, phase = high
// Initialize motors
for (int i = 0; i<4; i++)
initializeMotor(i, configWord);
for (int motor = 0; motor < 4; motor++)
{
pinMode( controlPins[motor], INPUT_PULLUP);
digitalWrite(controlPins[motor], HIGH);
}
//Set initial actuator settings to pull at 0 speed for safety
dir1 = 0; dir2 = 0; dir3 = 0; dir4 = 0; // Set direction
pwm1 = 0; pwm2 = 0; pwm3 = 0; pwm4 = 0; // Set speed (0-255)
motorOff(false);
Serial.begin(9600);
} // End setup
/* LOOP */
void loop()
{
//Serial.println("Loop");
// CONSOLE COMMANDS
bool serialProcessed = false;
while (Serial.available() > 0)
{
serialProcessed = true;
char incomingByte = Serial.read();
Serial.println("Got bit :");
Serial.println(incomingByte);
switch (incomingByte)
{
case '1' :
motorFlip(0);
break;
case '2' :
motorFlip(1);
break;
case '3' :
motorFlip(2);
break;
case '4' :
motorFlip(3);
break;
case 'p' :
toggleParallel();
case 'g' :
generalPattern();
break;
Serial.println("Pattern not recognized");
}
Serial.flush(); // Throw out anything not handled
}
if (parallel && serialProcessed)
{
delay(4000);
//motorOff(false);
for (int motor = 0; motor < 4; motor++)
{
//digitalWrite(mDIR[motor], 0);
analogWrite( mPWM[motor], 0);
}
//motorOff(true);
}
//////////////////////////////////////
// Commands from the ECU
//////////////////////////////////////
//Comands that come in along the pins
bool flipHappened = false;
for (int i = 0; i < 4; i++)
if (digitalRead(controlPins[i]) == LOW)
{
motorFlip(i);
flipHappened = true;
}
if (parallel && flipHappened)
{
delay(4000);
//motorOff(false);
for (int motor = 0; motor < 4; motor++)
{
//digitalWrite(mDIR[motor], 0);
analogWrite( mPWM[motor], 0);
}
//motorOff(true);
}
}//end void loop
/////////////////////////////////
// LOW LEVEL MOTOR FUNCTIONS ////
/////////////////////////////////
/*
Function: motorMove
Purpose: Commands one of the motors to move
in a direction at a certain speed.
Input: motor - which motor 0...3
dir - direction {0:down, 1:up}
speed - {0:slowest .. 255:fastest}
*/
void motorMove(int motor, int dir, int speed)
{
// Move motor
Serial.print("\nMoving ");
Serial.print(motor);
Serial.print(" in direction ");
Serial.print(dir);
Serial.print(" with speed ");
Serial.print(speed);
Serial.print("\n");
// motorOff(false);
digitalWrite(mDIR[motor], dir);
analogWrite( mPWM[motor], speed);
if (!parallel)
{
delay(3000);
digitalWrite(mDIR[motor], 0);
analogWrite( mPWM[motor], 0);
}
// motorOff(true);
// Store state
motorStates[motor] = dir;
}
void motorFlip(int motor)
{
int speed;
if (motorStates[motor] == UP)
motorStates[motor] = DOWN;
else
motorStates[motor] = UP;
speed = motorSpeed(motor, motorStates[motor]);
motorMove( motor, motorStates[motor], speed );
}
void toggleParallel()
{
Serial.print("\nToggling parllel mode");
parallel = !parallel;
if (parallel)
Serial.print("on\n");
else
Serial.print("off\n");
}
/*
Function: motorOff
Purpose: enable or disable motor output
true - off
false- on
*/
void motorOff(bool val)
{
if (val == true)
{
Serial.println("deactivating motors");
digitalWrite(ENABLE_MOTORS, HIGH); // HIGH = disabled
}
else
{
Serial.println("activating motors");
digitalWrite(ENABLE_MOTORS, LOW);
}
}
void initializeMotor(int motor, unsigned int configWord)
{
digitalWrite(mSS[motor], LOW);
SPI.transfer(lowByte(configWord));
SPI.transfer(highByte(configWord));
digitalWrite(mSS[motor], HIGH);
}
//////////////////////////////////////
// LOW LEVEL DIGITAL POT FUNCTIONS ////
//////////////////////////////////////
int digitalPotWrite(int value)
{
digitalWrite(CS, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}
//////////////////////////////////
// General Movement Patterns ////
//////////////////////////////////
void generalPattern()
{
Serial.println("General motions");
// motorMove(1,UP,255);
// motorMove(2,DOWN,128);
// motorMove(3,UP,255);
// motorMove(4,DOWN,128);
// delay(5000); // wait once all four motors are set
dir1 = DOWN;
pwm1 = 128;
digitalWrite(DIR_M1, dir1);
analogWrite( PWM_M1, pwm1);
dir2 = UP;
pwm2 = 255;
digitalWrite(DIR_M2, dir2);
analogWrite( PWM_M2, pwm2);
dir3 = DOWN;
pwm3 = 128;
digitalWrite(DIR_M3, dir3);
analogWrite( PWM_M3, pwm3);
dir4 = DOWN;
pwm4 = 255;
digitalWrite(DIR_M4, dir4);
analogWrite( PWM_M4, pwm4);
delay(5000);
}
//////////////////////////////////
// Customizing motors ////
//////////////////////////////////
/*
function: motorSpeed
purpose: Customizing rise and fall times of
specific motors
input:
motor (int),
integer specifying the identity of a motor of interest
direction (int)
integer specifying the direction a motor of interest will
move (up/down)
/*/
int motorSpeed(int motor, int direction)
{
int speed = 0;
switch (motor)
{
case 0:
if (direction == UP)
speed = 128;
else
speed = 256;
break;
case 1:
if (direction == UP)
speed = 128;
else
speed = 256;
break;
case 2:
if (direction == UP)
speed = 128;
else
speed = 256;
break;
case 3:
if (direction == UP)
speed = 128;
else
speed = 256;
break;
}
return speed;
}