-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot_ros.ino
179 lines (147 loc) · 3.74 KB
/
robot_ros.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
#include <ros.h>
#include <ros/time.h>
#include <sensor_msgs/LaserScan.h>
#include <nav_msgs/Odometry.h>
#include <math.h>
#include <SR04.h>
/************************
Exercise the motor using
the L293D chip
************************/
// Motor pins
#define LEFT_ENABLE 3
#define LEFT_DIRA 4
#define LEFT_DIRB 2
// Motor pins
#define RIGHT_ENABLE 5
#define RIGHT_DIRA 6
#define RIGHT_DIRB 7
// SR04 sensor pins
#define TRIG_RIGHT 9
#define ECHO_RIGHT 8
#define ECHO_CENTER 10
#define TRIG_CENTER 11
#define TRIG_LEFT 13
#define ECHO_LEFT 12
// Right and Left sensors are at 45'
SR04 sr_center(ECHO_CENTER,TRIG_CENTER);
SR04 sr_right(ECHO_RIGHT,TRIG_RIGHT);
SR04 sr_left(ECHO_LEFT,TRIG_LEFT);
enum motor_direction {
BACK,
FORWARD
};
enum side {
LEFT,
RIGHT
};
void set_direction(side s, motor_direction d)
{
const int dira = (s == LEFT) ? LEFT_DIRA : RIGHT_DIRA;
const int dirb = (s == LEFT) ? LEFT_DIRB : RIGHT_DIRB;
digitalWrite(dira, (d == BACK) ? HIGH : LOW); //one way
digitalWrite(dirb, (d == BACK) ? LOW : HIGH);
}
void move(motor_direction d, int power = 128)
{
analogWrite(LEFT_ENABLE, power); // enable on
analogWrite(RIGHT_ENABLE, power); // enable on
set_direction(LEFT, d);
set_direction(RIGHT, d);
}
void stop()
{
digitalWrite(LEFT_ENABLE, LOW);
digitalWrite(RIGHT_ENABLE, LOW);
}
// angle: -180 : 180 degrees
void turn(int angle, int power = 64)
{
analogWrite(LEFT_ENABLE, power);
analogWrite(RIGHT_ENABLE, power);
set_direction(LEFT, (angle < 0) ? BACK : FORWARD);
set_direction(RIGHT,(angle > 0) ? FORWARD : BACK);
delay(abs(angle));
stop();
}
// ROS stuff
ros::NodeHandle nh;
sensor_msgs::LaserScan scan;
ros::Publisher pub_scan("laser_scan", &scan);
//nav_msgs::Odometry odom;
//ros::Publisher pub_odom("odom", &odom);
const int num_ranges = 3; // number of range sensors
void setup() {
//---set pins for motors.
pinMode(LEFT_ENABLE,OUTPUT);
pinMode(LEFT_DIRA,OUTPUT);
pinMode(LEFT_DIRB,OUTPUT);
pinMode(RIGHT_ENABLE,OUTPUT);
pinMode(RIGHT_DIRA,OUTPUT);
pinMode(RIGHT_DIRB,OUTPUT);
// Set up ROS messaging system
//nh.getHardware()->SetBaud(500000);
nh.initNode();
nh.advertise(pub_scan);
// We pretend we have a laser scanner that sans -45 to +45 degrees with 3 measurment points
const float radians_for_45_degrees = PI/4.0;
scan.header.frame_id = "laser";
scan.angle_min = -radians_for_45_degrees;
scan.angle_max = radians_for_45_degrees;
scan.angle_increment = radians_for_45_degrees;
scan.time_increment = 0.025; // time btw scans => 25ms fixed
scan.range_min = 0.04;
scan.range_max = 2.0;
}
void publish_sensor_data()
{
// Take one reading at a time
// each measurment takes about 12 + 25ms (due to artifical delay in library)
// local storage becase scan msg uses a pointer and we dont want to alloc!
float ranges[num_ranges];
for (int i = 0; i < num_ranges; ++i)
{
long range = 0;
switch (i % 3) {
case 0:
range = sr_left.Distance();
break;
case 1:
range = sr_center.Distance();
break;
case 2:
range = sr_right.Distance();
break;
}
ranges[i] = range/100.0;
}
scan.header.stamp = nh.now();
scan.ranges = ranges;
scan.ranges_length = num_ranges;
// publish the reading
pub_scan.publish(&scan);
}
void loop() {
publish_sensor_data();
nh.spinOnce();
/* MOTOR CONTROL
if (center > 0 && center < 10)
{
stop();
Serial.println("STOP!");
}
else
{
move(FORWARD);
delay(500);
//turn(90);
//delay(1000);
}
Serial.print("left:");
Serial.print(left);
Serial.print(" center:");
Serial.print(center);
Serial.print(" right:");
Serial.println(right);
*/
}