1 | #include "Arduino.h" |
2 | #include "ard1.h" |
3 | #include "serial.h" |
4 | #include "reset.h" |
5 |
|
6 | #include "SoftwareSerial.h" |
7 |
|
8 |
|
9 | void oninput(String message); |
10 | SoftwareSerial roboclaw(0, 1); |
11 |
|
12 | // One time |
13 | void setup() { |
14 | pinMode(PIN_RESET, INPUT); |
15 | digitalWrite(PIN_RESET, LOW); |
16 | serial(); |
17 | roboclaw.begin(9600); |
18 | Serial.println("ARD1: Motor Controller Active"); |
19 | pinMode(PIN_BRAKES, OUTPUT); |
20 | pinMode(PIN_MR_D, OUTPUT); |
21 | pinMode(PIN_MR_R, OUTPUT); |
22 | pinMode(PIN_ML_D, OUTPUT); |
23 | pinMode(PIN_ML_R, OUTPUT); |
24 | pinMode(7, INPUT); |
25 | pinMode(4, OUTPUT); |
26 | digitalWrite(4, HIGH); |
27 | pinMode(PIN_CH_1, INPUT); |
28 | pinMode(PIN_CH_2, INPUT); |
29 | pinMode(PIN_CH_3, INPUT); |
30 | } |
31 |
|
32 | int brakes = 1; |
33 | int ml = 0; |
34 | int mr = 0; |
35 |
|
36 | void oninput (String message) { |
37 | Serial.print("ARD1: Received information - "); |
38 | Serial.println(message); |
39 | if (message == String("Restart")) { |
40 | Serial.println("ARD1: Restarting..."); |
41 | delay(500); |
42 | reset(); |
43 | } else if (message == String("b0")) { |
44 | Serial.println("ARD1: Turning brakes off"); |
45 | brakes = 0; |
46 | digitalWrite(PIN_BRAKES, HIGH); |
47 | } else if (message == String("b1")) { |
48 | Serial.println("ARD1: Turning brakes on"); |
49 | brakes = 1; |
50 | digitalWrite(PIN_MR_D, LOW); |
51 | digitalWrite(PIN_MR_R, LOW); |
52 | digitalWrite(PIN_ML_D, LOW); |
53 | digitalWrite(PIN_ML_R, LOW); |
54 | if (mr != 0 || ml != 0) delay(CHANGE_DELAY); |
55 | mr = 0; |
56 | ml = 0; |
57 | digitalWrite(PIN_BRAKES, LOW); |
58 | } |
59 | if (brakes == 1) return; |
60 | if (message == String("mr0")) { |
61 | Serial.println("ARD1: Turning MotorRight off"); |
62 | digitalWrite(PIN_MR_D, LOW); |
63 | digitalWrite(PIN_MR_R, LOW); |
64 | mr = 0; |
65 | } else if (message == String("mr1")) { |
66 | Serial.println("ARD1: Turning MotorRight on (drive)"); |
67 | digitalWrite(PIN_MR_R, LOW); |
68 | if (mr == -1) delay(CHANGE_DELAY); |
69 | mr = 1; |
70 | digitalWrite(PIN_MR_D, HIGH); |
71 | } else if (message == String("mr-1")) { |
72 | Serial.println("ARD1: Turning MotorRight on (reverse)"); |
73 | digitalWrite(PIN_MR_D, LOW); |
74 | if (mr == 1) delay(CHANGE_DELAY); |
75 | mr = -1; |
76 | digitalWrite(PIN_MR_R, HIGH); |
77 | } else if (message == String("ml0")) { |
78 | Serial.println("ARD1: Turning MotorLeft off"); |
79 | ml = 0; |
80 | digitalWrite(PIN_ML_D, LOW); |
81 | digitalWrite(PIN_ML_R, LOW); |
82 | } else if (message == String("ml1")) { |
83 | Serial.println("ARD1: Turning MotorLeft on (drive)"); |
84 | digitalWrite(PIN_ML_R, LOW); |
85 | if (ml == -1) delay(CHANGE_DELAY); |
86 | ml = 1; |
87 | digitalWrite(PIN_ML_D, HIGH); |
88 | } else if (message == String("ml-1")) { |
89 | Serial.println("ARD1: Turning MotorLeft on (reverse)"); |
90 | digitalWrite(PIN_ML_D, LOW); |
91 | if (ml == 1) delay(CHANGE_DELAY); |
92 | ml = -1; |
93 | digitalWrite(PIN_ML_R, HIGH); |
94 | } else if (message == String("m0")) { |
95 | Serial.println("ARD1: Turning both motors off"); |
96 | digitalWrite(PIN_MR_D, LOW); |
97 | digitalWrite(PIN_MR_R, LOW); |
98 | digitalWrite(PIN_ML_D, LOW); |
99 | digitalWrite(PIN_ML_R, LOW); |
100 | mr = 0; |
101 | ml = 0; |
102 | } else if (message == String("m1")) { |
103 | Serial.println("ARD1: Turning both motors on (Drive)"); |
104 | digitalWrite(PIN_MR_R, LOW); |
105 | digitalWrite(PIN_ML_R, LOW); |
106 | if (mr == -1 || ml == -1) delay(CHANGE_DELAY); |
107 | mr = 1; |
108 | ml = 1; |
109 | digitalWrite(PIN_MR_D, HIGH); |
110 | digitalWrite(PIN_ML_D, HIGH); |
111 | } else if (message == String("m-1")) { |
112 | Serial.println("ARD1: Turning both motors on (Reverse)"); |
113 | digitalWrite(PIN_MR_D, LOW); |
114 | digitalWrite(PIN_ML_D, LOW); |
115 | if (mr == 1 || ml == 1) delay(CHANGE_DELAY); |
116 | mr = -1; |
117 | ml = -1; |
118 | digitalWrite(PIN_MR_R, HIGH); |
119 | digitalWrite(PIN_ML_R, HIGH); |
120 | } |
121 | } |
122 |
|
123 | byte zero = 0; |
124 | // Repeatedly |
125 | void loop() { |
126 | if (Serial.available()) oninput(Serial.readStringUntil(';')); |
127 | roboclaw.write(1); |
128 | roboclaw.write(-127); |
129 | delay(2000); |
130 | roboclaw.write(64); |
131 | delay(1000); |
132 | roboclaw.write(127); |
133 | roboclaw.write(-1); |
134 | delay(2000); |
135 | roboclaw.write(-64); |
136 | delay(1000); |
137 | roboclaw.write(1); |
138 | roboclaw.write(-1); |
139 | delay(2000); |
140 | roboclaw.write(zero); |
141 | delay(1000); |
142 | roboclaw.write(127); |
143 | roboclaw.write(-127); |
144 | delay(2000); |
145 | roboclaw.write(zero); |
146 | delay(1000); |
147 | } |
148 |
|
149 |
|