-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMazeBot.ino
72 lines (57 loc) · 1.69 KB
/
AMazeBot.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
#include "Header.h"
void update() // update service routine
{
encUpdate(&oldEncoderLeft, &oldEncoderRight);
poseUpdate();
pidControl(realSpeed, desiredSpeed);
readSensorsIR(&distanceIR_L, &distanceIR_F, &distanceIR_R);
}
void setup()
{
// Setup pins
pinMode(LEFT_ENCODER_A, INPUT);
pinMode(LEFT_ENCODER_B, INPUT);
pinMode(RIGHT_ENCODER_A, INPUT);
pinMode(RIGHT_ENCODER_B, INPUT);
// Motors
pinMode(RIGHT_MOTOR_DIR, OUTPUT);
pinMode(RIGHT_MOTOR_PWM, OUTPUT);
pinMode(LEFT_MOTOR_DIR, OUTPUT);
pinMode(LEFT_MOTOR_PWM, OUTPUT);
// Initialize oldEncoder values
encUpdate(&oldEncoderLeft, &oldEncoderRight);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(25);
// Setup ROS
setupROS();
desiredSpeed = cmd_vel(0.00, 0.0); // pass desired linear velocity (m/s) and angular velocitiy (rad/s)
// Start motors
digitalWrite(RIGHT_MOTOR_DIR, FORWARD);
analogWrite(RIGHT_MOTOR_PWM, 0);
digitalWrite(LEFT_MOTOR_DIR, FORWARD);
analogWrite(LEFT_MOTOR_PWM, 0);
// Initialize timestamp
timestamp = millis();
}
void loop()
{
// Polling version of the code
if ((millis() - timestamp) >= 1000 / UPDATE_FREQUENCY)
{
update(); // update robot
if(rosOK() == true) {
publish(); // Publish all ROS messages
}
else {
desiredSpeed = cmd_vel(0.00, 0.0); // Stop locally because message not getting through anymore
}
if (cmd_vel_timeout >= 10)
{
desiredSpeed = cmd_vel(0.00, 0.0);
cmd_vel_pub.publish(&stop_msg); // Stop robot after timeout when not receiving any new messages
}
cmd_vel_timeout++;
timestamp = millis();
nh.spinOnce(); // Receive messages
}
}