Revision 6331ce9f
and .... now it compiles
arduino/RCbuggyMega/RCbuggyMega.ino | ||
---|---|---|
10 | 10 |
|
11 | 11 |
*/ |
12 | 12 |
|
13 |
//#include <SoftwareSerial.h>
|
|
13 |
#include <SoftwareSerial.h> |
|
14 | 14 |
#include <Servo.h> |
15 |
#include "brake.h" |
|
16 |
#include "xbee.h" |
|
15 | 17 |
|
16 | 18 |
#define BRAKE_PIN 8 |
17 | 19 |
#define BRAKE_INDICATOR_PIN 5 |
... | ... | |
19 | 21 |
|
20 | 22 |
#define SERVO_PIN 9 |
21 | 23 |
|
24 |
unsigned long timer = 0L; |
|
25 |
char data; |
|
26 |
|
|
27 |
|
|
22 | 28 |
Servo myservo; // create servo object to control a servo |
23 | 29 |
|
24 | 30 |
void setup() { |
... | ... | |
28 | 34 |
xbee_init(XBEE_LED_PIN); |
29 | 35 |
|
30 | 36 |
// initialize the brake with brake pin and led brake pin |
31 |
brake_init(brakePin, brakeLedPin);
|
|
37 |
brake_init(BRAKE_PIN, BRAKE_INDICATOR_PIN);
|
|
32 | 38 |
|
33 | 39 |
|
34 | 40 |
myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object |
35 |
myservo.write(STEEING_CENTER);
|
|
41 |
myservo.write(getSteeringAngle());
|
|
36 | 42 |
} |
37 | 43 |
|
38 | 44 |
void loop() { |
... | ... | |
76 | 82 |
|
77 | 83 |
int steeringAngle = getSteeringAngle(); |
78 | 84 |
|
79 |
s // sets the servo position
|
|
85 |
// sets the servo position |
|
80 | 86 |
myservo.write(steeringAngle); |
81 | 87 |
} |
82 | 88 |
|
arduino/RCbuggyMega/brake.h | ||
---|---|---|
1 |
/** |
|
2 |
* @file brake.h |
|
3 |
* @author Audrey Yeoh (ayeoh) |
|
4 |
* @author Matt Sebek (msebek) |
|
5 |
* |
|
6 |
* Initializes, raises, drops, and gets-state of |
|
7 |
* the brakes. |
|
8 |
* |
|
9 |
*/ |
|
10 |
#ifndef _BRAKE_H_ |
|
11 |
#define _BRAKE_H_ |
|
12 |
|
|
13 |
void brake_init(int brakePin, int indicatorLedPin); |
|
14 |
|
|
15 |
void brake_raise(); |
|
16 |
|
|
17 |
void brake_drop(); |
|
18 |
|
|
1 |
/** |
|
2 |
* @file brake.h |
|
3 |
* @author Audrey Yeoh (ayeoh) |
|
4 |
* @author Matt Sebek (msebek) |
|
5 |
* |
|
6 |
* Initializes, raises, drops, and gets-state of |
|
7 |
* the brakes. |
|
8 |
* |
|
9 |
*/ |
|
10 |
#ifndef _BRAKE_H_ |
|
11 |
#define _BRAKE_H_ |
|
12 |
#ifdef __cplusplus |
|
13 |
extern "C"{ |
|
14 |
#endif |
|
15 |
|
|
16 |
void brake_init(int brakePin, int indicatorLedPin); |
|
17 |
|
|
18 |
void brake_raise(); |
|
19 |
|
|
20 |
void brake_drop(); |
|
21 |
|
|
22 |
#ifdef __cplusplus |
|
23 |
} // extern "C" |
|
24 |
#endif |
|
19 | 25 |
#endif |
arduino/RCbuggyMega/xbee.c | ||
---|---|---|
1 |
/** |
|
2 |
* Code relating to something I'm not sure yet |
|
3 |
* @author Audrey Yeoh (ayeoh) |
|
4 |
* |
|
5 |
*/ |
|
6 |
|
|
7 |
#include <Arduino.h> |
|
8 |
#include "xbee.h" |
|
9 |
|
|
10 |
#define STEERING_CENTER 133 |
|
11 |
|
|
12 |
// holds the data read from the serial xbee |
|
13 |
static char data; |
|
14 |
|
|
15 |
// holds the message itself |
|
16 |
static String message; |
|
17 |
|
|
18 |
static int pingPong = 1; |
|
19 |
static int startFound = 0; |
|
20 |
static int midFound = 0; |
|
21 |
static int endFound = 1; |
|
22 |
static char intbuf[32]; |
|
23 |
|
|
24 |
static int steeringAngle = STEERING_CENTER; |
|
25 |
static int brake = 0; |
|
26 |
|
|
27 |
static int led; |
|
28 |
|
|
29 |
void xbee_init(int ledPin){ |
|
30 |
pinMode(ledPin, OUTPUT); |
|
31 |
led = ledPin; |
|
32 |
} |
|
33 |
|
|
34 |
// parses the data from the xbee serial |
|
35 |
void parse(char data){ |
|
36 |
switch(data){ |
|
37 |
case 'A': |
|
38 |
startFound = 1; |
|
39 |
midFound = 0; |
|
40 |
endFound = 0; |
|
41 |
message = ""; |
|
42 |
break; |
|
43 |
|
|
44 |
case 'B': |
|
45 |
startFound = 0; |
|
46 |
midFound = 1; |
|
47 |
endFound = 0; |
|
48 |
break; |
|
49 |
case 'C': |
|
50 |
startFound = 0; |
|
51 |
midFound = 0; |
|
52 |
endFound =1; |
|
53 |
break; |
|
54 |
default: // all other cases |
|
55 |
if (startFound){ |
|
56 |
message = message + data; |
|
57 |
|
|
58 |
}else if (midFound){ |
|
59 |
if (data == '1'){ |
|
60 |
brake = 1; |
|
61 |
}else{ |
|
62 |
brake = 0; |
|
63 |
} |
|
64 |
message.toCharArray(intbuf, sizeof(intbuf)); |
|
65 |
steeringAngle = atoi(intbuf); |
|
66 |
} |
|
67 |
break; |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
|
|
72 |
int getSteeringAngle(){ |
|
73 |
return steeringAngle; |
|
74 |
} |
|
75 |
|
|
76 |
int getBrake(){ |
|
77 |
return brake; |
|
78 |
} |
|
79 |
|
|
80 |
// flops an led everytime a message is received. |
|
81 |
// Helps to debug whether messages are received |
|
82 |
void debugMessage(){ |
|
83 |
if (pingPong == 0){ |
|
84 |
digitalWrite(led, LOW); |
|
85 |
}else{ |
|
86 |
digitalWrite(led, HIGH); |
|
87 |
} |
|
88 |
|
|
89 |
pingPong = 1 - pingPong; |
|
90 |
} |
arduino/RCbuggyMega/xbee.cpp | ||
---|---|---|
1 |
/** |
|
2 |
* Code relating to something I'm not sure yet |
|
3 |
* @author Audrey Yeoh (ayeoh) |
|
4 |
* |
|
5 |
*/ |
|
6 |
|
|
7 |
#include <Arduino.h> |
|
8 |
#include "xbee.h" |
|
9 |
|
|
10 |
#define STEERING_CENTER 133 |
|
11 |
|
|
12 |
// holds the data read from the serial xbee |
|
13 |
static char data; |
|
14 |
|
|
15 |
// holds the message itself |
|
16 |
static String message; |
|
17 |
|
|
18 |
static int pingPong = 1; |
|
19 |
static int startFound = 0; |
|
20 |
static int midFound = 0; |
|
21 |
static int endFound = 1; |
|
22 |
static char intbuf[32]; |
|
23 |
|
|
24 |
static int steeringAngle = STEERING_CENTER; |
|
25 |
static int brake = 0; |
|
26 |
|
|
27 |
static int led; |
|
28 |
|
|
29 |
void xbee_init(int ledPin){ |
|
30 |
pinMode(ledPin, OUTPUT); |
|
31 |
led = ledPin; |
|
32 |
} |
|
33 |
|
|
34 |
// parses the data from the xbee serial |
|
35 |
void parse(char data){ |
|
36 |
switch(data){ |
|
37 |
case 'A': |
|
38 |
startFound = 1; |
|
39 |
midFound = 0; |
|
40 |
endFound = 0; |
|
41 |
message = ""; |
|
42 |
break; |
|
43 |
|
|
44 |
case 'B': |
|
45 |
startFound = 0; |
|
46 |
midFound = 1; |
|
47 |
endFound = 0; |
|
48 |
break; |
|
49 |
case 'C': |
|
50 |
startFound = 0; |
|
51 |
midFound = 0; |
|
52 |
endFound =1; |
|
53 |
break; |
|
54 |
default: // all other cases |
|
55 |
if (startFound){ |
|
56 |
message = message + data; |
|
57 |
|
|
58 |
}else if (midFound){ |
|
59 |
if (data == '1'){ |
|
60 |
brake = 1; |
|
61 |
}else{ |
|
62 |
brake = 0; |
|
63 |
} |
|
64 |
message.toCharArray(intbuf, sizeof(intbuf)); |
|
65 |
steeringAngle = atoi(intbuf); |
|
66 |
} |
|
67 |
break; |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
|
|
72 |
int getSteeringAngle(){ |
|
73 |
return steeringAngle; |
|
74 |
} |
|
75 |
|
|
76 |
int getBrake(){ |
|
77 |
return brake; |
|
78 |
} |
|
79 |
|
|
80 |
// flops an led everytime a message is received. |
|
81 |
// Helps to debug whether messages are received |
|
82 |
void debugMessage(){ |
|
83 |
if (pingPong == 0){ |
|
84 |
digitalWrite(led, LOW); |
|
85 |
}else{ |
|
86 |
digitalWrite(led, HIGH); |
|
87 |
} |
|
88 |
|
|
89 |
pingPong = 1 - pingPong; |
|
90 |
} |
Also available in: Unified diff