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