-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathred-rover-firmware.ino
314 lines (269 loc) · 7.95 KB
/
red-rover-firmware.ino
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
/*
* Author: Brad Bazemore
* Licence: MIT
*/
#include <FastGPIO.h>
#include <Servo.h>
#include <ros.h>
#include <std_msgs/Float64.h>
#include <std_msgs/UInt8.h>
/*
* Tire is 30.48cm radius
* Circumference is 191.51cm
* Encoder P/R is 1024
* Distance is 0.18cm per pulse
* Distance per window 128P 23.9 cm
*
* TODO Add dead band
*/
//////////////////////////////////////
//Config
//////////////////////////////////////
// Digital pins
#define A 5 // pin number of A pulse
#define B 6 // pin number of B pulse
#define ACTUATOR_PIN 3 // pin for the linear servo singnal
#define THROTTLE_PIN 4 // pin for throttle servo
#define LEFT_PIN 7 // pin for relay to turn left
#define RIGHT_PIN 8 // pin for relay to turn right
// Analog pins
#define PIVOT_PIN 0 // pin for the potentiometer
// Limits
#define THROTTLE_MAX 120
#define THROTTLE_MIN 60
#define ACTUATOR_MAX 138
#define ACTUATOR_MIN 65
#define ACTUATOR_SCALE 90
// Homes
#define THROTTLE_HOME THROTTLE_MIN
#define ACTUATOR_HOME 90
//////////////////////////////////////
//ROS
//////////////////////////////////////
// ros node object
ros::NodeHandle nh;
std_msgs::Float64 vel; // the ros message for out velocity
ros::Publisher encoder_pub("driver/encoder_velocity", &vel); // the publisher of the velocty
std_msgs::Float64 pivot; // ros meesage for the amount of pivot
ros::Publisher pivot_pub("driver/pivot", &pivot); // publisher of pivot
void actuator_callback(const std_msgs::Float64 &cmd_msg); // method def used for actuator call back
ros::Subscriber<std_msgs::Float64> actuator_sub("driver/linear_drive_actuator", actuator_callback);
void throttle_callback(const std_msgs::UInt8 &cmd_msg); // method def used for actuator call back
ros::Subscriber<std_msgs::UInt8> throttle_sub("driver/throttle", throttle_callback);
void articulation_callback(const std_msgs::Float64 &cmd_msg); // methd def use for articulation call back
ros::Subscriber<std_msgs::Float64> articulation_sub("driver/articulation_relay", articulation_callback);
//////////////////////////////////////
//Hardware Connections
//////////////////////////////////////
Servo actuator; // servo object for the linear actuator
Servo throttle; // servo object for the engine throttle
//////////////////////////////////////
//Global Variables
//////////////////////////////////////
// last value of the a and b pulses
bool A_last_val;
bool B_last_val;
char state; // directional state of the encoder
int count; // number of times a pulse has been seen
unsigned long last_time; // last time we saw 128 pulses
unsigned long loop_time; // time that the entire loop has ran
float distance; // distance we have gone
float velocity; // out velocity
unsigned long ros_rate; // the rate in milliseconds to refresh ros
float angle; // the angle of pivot
void setup() {
// start ros, start publisher, and subscriber
nh.initNode();
nh.advertise(encoder_pub);
nh.advertise(pivot_pub);
nh.subscribe(actuator_sub);
nh.subscribe(throttle_sub);
nh.subscribe(articulation_sub);
// set up servos
actuator.attach(ACTUATOR_PIN); // set pin to be used for actuator
throttle.attach(THROTTLE_PIN); // set the pin for the throttle servo
actuator.write(ACTUATOR_HOME); // set everyone to home that is safe
throttle.write(THROTTLE_HOME);
// set up turning relays
pinMode(LEFT_PIN, OUTPUT); // set io pin for left relay to output
pinMode(RIGHT_PIN, OUTPUT); // set io pin for right relay to output
digitalWrite(LEFT_PIN, LOW); // make sure they are off at start
digitalWrite(RIGHT_PIN, LOW);
// status light
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
// add pins A and B to the fast GPIO, we use this to not miss encoder pulses
FastGPIO::Pin<A>::setInput();
FastGPIO::Pin<B>::setInput();
// init values for the A and B last pulses
A_last_val = false;
B_last_val = false;
// set the rate for ros to update at 100ms
ros_rate = 100;
// more init values
state = 'F';
count = 0;
distance = 0;
velocity = 0;
angle = 0;
// init the loop timers
last_time = millis();
loop_time = millis();
}
/*
* parms:
*
* return: void
*
* This is the main loop of the program. It will get the velocity, direction
* and distance from the encoder. It will then update ros every ros_rate
*/
void loop() {
//safty code, will stop everything if loss connection to ROS
while(!nh.connected()){
digitalWrite(13, LOW);
allStop();
nh.spinOnce();
}
digitalWrite(13, HIGH);
bool change = false;
bool A_val = false;
bool B_val = false;
while(millis()-loop_time < ros_rate){
A_val = FastGPIO::Pin<A>::isInputHigh();
B_val = FastGPIO::Pin<B>::isInputHigh();
if((A_last_val != A_val) || (B_last_val != B_val)){
count++;
direction(A_val,B_val);
}
if(count > 128){
change = true;
distance += 23.9;
if(state == 'F'){
velocity = (distance / (millis()-last_time))/100;
}else{
velocity = ((distance / (millis()-last_time))/100)*-1;
}
count = 0;
last_time = millis();
//Update data
vel.data=velocity;
}
if(!change){
vel.data = 0;
}
}
loop_time = millis();
get_angle();
pivot.data = angle;
encoder_pub.publish(&vel);
pivot_pub.publish(&pivot);
nh.spinOnce();
}
/*
* params:
* bool A_val: this is the current value of the A pulse
* bool B_val: this is the current value of the B pulse
*
* return: void
*
* This method finds the direction of the encoder. This is done by finding if the A
* or B puls leads. If A leads then we are doing clock wise, if B leads then we are
* going counter clock wise.
*/
void direction(bool A_val, bool B_val){
if((A_last_val && !B_last_val) && (A_val && B_val)){
state = 'F';
}else if((!A_last_val && B_last_val) && (A_val && B_val)){
state = 'B';
}
A_last_val = A_val;
B_last_val = B_val;
}
/*
* param:
*
* return: void
*
* Read the pivot sensor. Conver to angle in degreese.
* Thanks to Tim for find out this equation.
*/
void get_angle(){
float temp = analogRead(PIVOT_PIN);
angle = 0.08996 * temp - 41.9215;
}
/*
* param:
* std_msgs::Float64 &cmd_msgs: data from ROS
*
* Move the actuator on the hydrolic pump
*/
void actuator_callback(const std_msgs::Float64 &cmd_msg){
if((cmd_msg.data+ACTUATOR_SCALE) > ACTUATOR_MAX){
nh.logwarn("DH");
actuator.write(ACTUATOR_MAX);
}else if((cmd_msg.data+ACTUATOR_SCALE) < ACTUATOR_MIN){
actuator.write(ACTUATOR_MIN);
nh.logwarn("DL");
}else{
actuator.write((cmd_msg.data+ACTUATOR_SCALE));
}
}
/*
* param:
* std_msgs::UInt16 &cmd_msgs: this is the message coming from ROS
*
* return: void
*
* Change the throttle
*/
void throttle_callback(const std_msgs::UInt8 &cmd_msg){
if(cmd_msg.data > THROTTLE_MAX){
throttle.write(THROTTLE_MAX);
nh.logwarn("TH");
}else if(cmd_msg.data < THROTTLE_MIN){
throttle.write(THROTTLE_MIN);
nh.logwarn("TL");
}else{
throttle.write(cmd_msg.data);
}
}
/*
* param:
*
* return: void
*
* TODO idk how this should be done as turning is booblean operation.
* Maybe we should look at having this be set up to have a number come in but
* it just turns on and off based on a threshold? IDk, just a thought.
*/
void articulation_callback(const std_msgs::Float64 &cmd_msg){
if((cmd_msg.data >= 0) && (cmd_msg.data < 0.5)){ // turn left
digitalWrite(LEFT_PIN, HIGH);
digitalWrite(RIGHT_PIN, LOW);
}else if((cmd_msg.data < 1.5) && (cmd_msg.data >= 0.5)){ // don't turn
digitalWrite(LEFT_PIN, LOW);
digitalWrite(RIGHT_PIN, LOW);
}else if((cmd_msg.data <= 2) && (cmd_msg.data >= 1.5)){ // turn right
digitalWrite(LEFT_PIN, LOW);
digitalWrite(RIGHT_PIN, HIGH);
}else if(cmd_msg.data > 2 || cmd_msg.data < 0){
nh.logwarn("AO");
digitalWrite(LEFT_PIN, LOW);
digitalWrite(RIGHT_PIN, LOW);
}
}
/*
* parms:
*
* return: void
*
* Stops everything
*/
void allStop(){
digitalWrite(LEFT_PIN, LOW);
digitalWrite(RIGHT_PIN, LOW);
actuator.write(ACTUATOR_HOME);
throttle.write(THROTTLE_HOME);
digitalWrite(13, LOW);
}