source: robotics/car/firmware/car/car.ino @ 4c79bbb

Last change on this file since 4c79bbb was 4c79bbb, checked in by Steve Castellotti <sc@…>, 7 years ago
  • tweaking in lot
  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2
3Puzzlebox - Robotics - Car
4
5Puzzlebox Robotics car driving sketch.
6
7Copyright Puzzlebox Productions, LLC (2014)
8
9This code is released under the GNU Pulic License (GPL) version 2
10
11This software is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15You should have received a copy of the GNU General Public
16License along with this code; if not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19For more information about this licence please refer to http://www.gnu.org/copyleft/gpl.html
20
21For more details about the product please check http://puzzlebox.info
22
23Original Author: Steve Castellotti <sc@puzzlebox.info>
24
25Modified 2014-06-16
26by Steve Castellotti <sc@puzzlebox.info>
27
28*/
29
30#include <Servo.h>
31
32#define DEBUG_OUTPUT 1 // 1 for debug
33
34#define SERVO_PIN_BRAKE 7
35#define SERVO_PIN_DIRECTION 8
36#define SERVO_PIN_TURN 9
37
38#define POSITION_BRAKE_STOP 60
39#define POSITION_BRAKE_GO 150
40
41#define POSITION_DIRECTION_LEFT 150
42#define POSITION_DIRECTION_STRAIGHT 90
43#define POSITION_DIRECTION_RIGHT 30
44
45#define POSITION_TURN_STOP 30
46#define POSITION_TURN_GO 170
47
48#define TIME_DRIVE_FORWARD 10000 // Drive forward for two seconds per detection
49#define TIME_TURN 3000 // Turn wheel for one second per detection
50#define TIME_TURN_DRIVE 4000 // Drive forward to two seconds while turning
51
52Servo servoBrake;
53Servo servoDirection;
54Servo servoTurn;
55
56char _command;
57char lastCommand = '2'; // stop
58
59
60// ################################################################
61
62/////////
63//SETUP//
64/////////
65
66void setup() {
67       
68  Serial.begin(115200);
69
70  servoBrake.attach(SERVO_PIN_BRAKE);
71  servoDirection.attach(SERVO_PIN_DIRECTION);
72  servoTurn.attach(SERVO_PIN_TURN);
73 
74  servoBrake.write(POSITION_BRAKE_STOP);
75  servoDirection.write(POSITION_DIRECTION_STRAIGHT);
76  servoTurn.write(POSITION_TURN_STOP);
77 
78
79} // setup
80
81
82// ################################################################
83
84/////////////
85//MAIN LOOP//
86/////////////
87
88void loop() {
89
90  parseSerialInput();
91
92} // Main loop
93
94
95// ################################################################
96
97int parseValue() {
98 
99  char inByte=0;
100  int a=0;
101  int b=0;
102  int c=0;
103  int newValue=0;
104
105  while (Serial.available() == 0);
106  inByte = Serial.read() - '0';
107  a = inByte;
108
109  while (Serial.available() == 0);
110  inByte = Serial.read() - '0';
111  b = inByte;
112
113  while (Serial.available() == 0);
114  inByte = Serial.read() - '0';
115  c = inByte;
116
117  newValue = (a * 100) + (b * 10) + c;
118
119  return(newValue);
120
121} // parseValue
122
123
124// ################################################################
125
126void parseSerialInput() {
127
128  if (Serial.available() > 0)  {
129
130    _command = Serial.read();
131
132    #if DEBUG_OUTPUT
133      Serial.print("Serial.read(): ");
134      Serial.println(_command);
135    #endif
136
137    parseCommand(_command);
138   
139    if (_command != '0') {
140      // Don't directly set lastCommand to "repeat"
141      lastCommand = _command;
142    }
143   
144  }
145
146} // parseSerialInput()
147
148
149// ################################################################
150
151void parseCommand(char command) {
152
153    switch (command) {
154
155      case '0':  driveRepeat(); break;
156      case '1':  driveForward(); break;
157      case '2':  driveStop(); break;
158      case '3':  driveLeft(); break;
159      case '4':  driveRight(); break;
160     
161      case '.':  driveRepeat(); break;
162      case 'f':  driveForward(); break;
163      case 's':  driveStop(); break;
164      case 'l':  driveLeft(); break;
165      case 'r':  driveRight(); break;
166
167      case 'F':  driveForward(); break;
168      case 'S':  driveStop(); break;
169      case 'L':  driveLeft(); break;
170      case 'R':  driveRight(); break;
171
172
173    }
174}
175
176// ################################################################
177
178void driveRepeat() {
179 
180  #if DEBUG_OUTPUT
181    Serial.println("driveRepeat()");
182  #endif
183 
184  parseCommand(lastCommand);
185
186}
187
188// ################################################################
189
190void driveForward() {
191
192  #if DEBUG_OUTPUT
193    Serial.println("driveForward()");
194  #endif
195 
196  servoBrake.write(POSITION_BRAKE_GO);
197 
198  delay(TIME_DRIVE_FORWARD);
199 
200  servoBrake.write(POSITION_BRAKE_STOP);
201 
202}
203
204// ################################################################
205
206void driveStop() {
207
208  #if DEBUG_OUTPUT
209    Serial.println("driveStop()");
210  #endif
211 
212  servoBrake.write(POSITION_BRAKE_STOP);
213 
214}
215
216// ################################################################
217
218void driveLeft() {
219
220  #if DEBUG_OUTPUT
221    Serial.println("driveLeft()");
222  #endif
223 
224  // Select direction
225  servoDirection.write(POSITION_DIRECTION_LEFT);
226 
227  // Turn wheel
228  servoTurn.write(POSITION_TURN_GO);
229  delay(TIME_TURN);
230  servoTurn.write(POSITION_TURN_STOP);
231 
232  // Release brake
233  servoBrake.write(POSITION_BRAKE_GO);
234 
235  // Drive while turning
236  delay(TIME_TURN_DRIVE);
237 
238  // Apply brake
239  servoBrake.write(POSITION_BRAKE_STOP);
240 
241  // Reset wheel
242//  servoDirection.write(POSITION_DIRECTION_RIGHT);
243//  servoTurn.write(POSITION_TURN_GO);
244//  delay(TIME_TURN);
245//  servoTurn.write(POSITION_TURN_STOP);
246 
247  // Reset direction
248  servoDirection.write(POSITION_DIRECTION_STRAIGHT);
249 
250}
251
252// ################################################################
253
254void driveRight() {
255
256  #if DEBUG_OUTPUT
257    Serial.println("driveRight()");
258  #endif
259 
260  // Select direction
261  servoDirection.write(POSITION_DIRECTION_RIGHT);
262 
263  // Turn wheel
264  servoTurn.write(POSITION_TURN_GO);
265  delay(TIME_TURN);
266  servoTurn.write(POSITION_TURN_STOP);
267 
268  // Release brake
269  servoBrake.write(POSITION_BRAKE_GO);
270 
271  // Drive while turning
272  delay(TIME_TURN_DRIVE);
273 
274  // Apply brake
275  servoBrake.write(POSITION_BRAKE_STOP);
276 
277  // Reset wheel
278//  servoDirection.write(POSITION_DIRECTION_LEFT);
279//  servoTurn.write(POSITION_TURN_GO);
280//  delay(TIME_TURN);
281//  servoTurn.write(POSITION_TURN_STOP);
282 
283  // Reset direction
284  servoDirection.write(POSITION_DIRECTION_STRAIGHT);
285 
286}
287
288// ################################################################
Note: See TracBrowser for help on using the repository browser.