-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlidarbot.cc
218 lines (193 loc) · 5.34 KB
/
lidarbot.cc
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
//
// C++ interface to miiboo driver and lidar
// Written by Eric Gregori
//
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <memory>
#include <unistd.h>
#include "lidarbot.h"
// Break the scan into SECTORS
#define SCAN_START 180
#define SCAN_END 540
#define CENTER (((SCAN_END-SCAN_START)/2)+SCAN_START)
#define WIDTH 60 // degrees*2
#define R_SECTOR_START (CENTER-WIDTH-(WIDTH/2))
#define R_SECTOR_END (CENTER-(WIDTH/2))
#define L_SECTOR_START (CENTER+(WIDTH/2))
#define L_SECTOR_END (CENTER+WIDTH+(WIDTH/2))
#define F_SECTOR_START R_SECTOR_END
#define F_SECTOR_END L_SECTOR_START
using namespace std;
void LidarBot::ReadLidarRaw(void)
{
if(laser->doProcessSimple(scan, hardError))
{
int size = (int)scan.ranges.size();
SECTOR tmp;
furthest.raw = 0.0;
closest.raw = 16.0;
points.clear();
for(int i=0; i<size; ++i)
{
float range = scan.ranges[i];
// Find furthest distance
if (range > furthest.raw)
{
furthest.raw = range;
furthest.point = i;
}
// Find closest distance
if (range > 0.0 && range < closest.raw)
{
closest.raw = range;
closest.point = i;
}
tmp.max = false;
tmp.type = '*';
tmp.point = i;
tmp.raw = range;
tmp.norm = range;
if(i >= L_SECTOR_START && i < L_SECTOR_END)
tmp.type = 'L';
else if(i > R_SECTOR_START && i <= R_SECTOR_END)
tmp.type = 'R';
else if(i >= F_SECTOR_START && i <= F_SECTOR_END)
tmp.type = 'F';
else
tmp.type = '*';
points.push_back(tmp);
}
points[furthest.point].max = true;
if(furthest.raw == 0.0)
{
throw(runtime_error("max 0"));
}
// Normalize
for(int i=0; i<(int)points.size(); ++i)
{
points[i].norm /= furthest.raw;
}
}
}
// Format points into l,f,r sectors
void LidarBot::GetSectors(SECTOR *left, SECTOR *forward, SECTOR *right)
{
float leftc, rightc, forwardc;
int pointc = (int)points.size();
// Get data from Lidar
leftc = 0.0;
rightc = 0.0;
forwardc = 0.0;
left->norm = 0.0;
right->norm = 0.0;
forward->norm = 0.0;
left->raw = 0.0;
right->raw = 0.0;
forward->raw = 0.0;
if(pointc > 0)
{
for(int i=0; i<(int)points.size(); ++i)
{
if(points[i].raw > 0.0)
{
if(points[i].type == 'L')
{
left->raw += points[i].raw;
left->norm += points[i].norm;
leftc += 1.0;
}
if(points[i].type == 'F')
{
forward->raw += points[i].raw;
forward->norm += points[i].norm;
forwardc += 1.0;
}
if(points[i].type == 'R')
{
right->raw += points[i].raw;
right->norm += points[i].norm;
rightc += 1.0;
}
}
}
left->raw /= leftc;
left->norm /= leftc;
right->raw /= rightc;
right->norm /= rightc;
forward->raw /= forwardc;
forward->norm /= forwardc;
}
}
// Visualize the data in a 40 x 40 space
void LidarBot::VisualizeRanges(void)
{
int points_per_column = points.size()/40;
int k, i, id;
for(i=0; i<((int)points.size()-points_per_column); )
{
float max = 0.0;
for(k=0, id=0; k<points_per_column; ++k)
{
if(points[i].norm > max)
{
max = points[i].norm;
id = i;
}
++i;
}
printf("\n");
for(k=0; k<(int)(80.0*max); ++k)
{
printf("%c", points[id].type);
}
if(points[i].max == true)
printf(">");
printf(" - %f", max);
}
}
LidarBot::LidarBot(char *motor_port, char *lidar_port)
{
printf("LidarBot Constructor\n");
l_sector_start = L_SECTOR_START;
r_sector_start = R_SECTOR_START;
f_sector_start = F_SECTOR_START;
l_sector_end = L_SECTOR_END;
r_sector_end = R_SECTOR_END;
f_sector_end = F_SECTOR_END;
// Configure lidar driver
printf("\n\nInstantiate lidar driver\n");
laser = new CYdLidar();
// Instantiates lidar
laser->setSerialPort(lidar_port);
laser->setSerialBaudrate(115200);
laser->setIntensities(0);
laser->initialize();
if(motor_port != NULL)
{
// Instantiate the motor driver
printf("Instantiating motor driver\n");
miiboo_object = new miiboo_driver(motor_port);
miiboo_object->move((unsigned char *)"s");
}
else
{
printf("MOTOR DRIVER DISABLED!!!\n");
miiboo_object = NULL;
}
}
LidarBot::~LidarBot()
{
printf("\n\nShutting down robot\n\n");
if(miiboo_object != NULL)
miiboo_object->move((unsigned char *)"s");
laser->turnOff();
laser->disconnecting();
sleep(1);
delete laser;
if(miiboo_object != NULL)
delete miiboo_object;
sleep(1);
}