4
4
#include < time.h>
5
5
6
6
#include " myagv_odometry/myAGV.h"
7
- #include " std_msgs/Int8.h"
8
7
9
8
const unsigned char header[2 ] = { 0xfe , 0xfe };
10
9
11
10
boost::asio::io_service iosev;
11
+
12
12
boost::asio::serial_port sp (iosev, " /dev/ttyAMA2" );
13
13
14
14
boost::array<double , 36 > odom_pose_covariance = {
@@ -26,16 +26,6 @@ boost::array<double, 36> odom_twist_covariance = {
26
26
0 , 0 , 0 , 0 , 1e6 , 0 ,
27
27
0 , 0 , 0 , 0 , 0 , 1e-9 } };
28
28
29
- void send ()
30
- {
31
- ;
32
- }
33
-
34
- void receive ()
35
- {
36
- ;
37
- }
38
-
39
29
MyAGV::MyAGV ()
40
30
{
41
31
x = 0.0 ;
@@ -69,7 +59,7 @@ bool MyAGV::init()
69
59
pub_imu = n.advertise <sensor_msgs::Imu>(" imu_data" , 20 );
70
60
pub_imu_raw = n.advertise <sensor_msgs::Imu>(" imu_raw_data" , 20 );
71
61
pub_odom = n.advertise <nav_msgs::Odometry>(" odom" , 50 ); // used to be 50
72
- pub_v = n.advertise <std_msgs::Int8 >(" Voltage" , 1000 );
62
+ pub_voltage = n.advertise <std_msgs::Float32 >(" Voltage" , 10 );
73
63
restore (); // first restore,abort current err,don't restore
74
64
return true ;
75
65
}
@@ -116,7 +106,7 @@ bool MyAGV::readSpeed()
116
106
int i, length = 0 , count = 0 ;
117
107
unsigned char checkSum;
118
108
unsigned char buf_header[1 ] = {0 };
119
- unsigned char buf[27 ] = {0 };
109
+ unsigned char buf[TOTAL_RECEIVE_SIZE ] = {0 };
120
110
121
111
size_t ret;
122
112
boost::system ::error_code er2;
@@ -156,18 +146,18 @@ bool MyAGV::readSpeed()
156
146
restoreRun ();
157
147
return false ;
158
148
}
159
- if (ret != 27 ) {
149
+ if (ret != TOTAL_RECEIVE_SIZE ) {
160
150
ROS_ERROR (" Read error %zu" ,ret);
161
151
return false ;
162
152
}
163
153
164
154
int index = 0 ;
165
155
int check = 0 ;// ilter time older than imu message buffer
166
- for (int i = 0 ; i < 26 ; ++i)
156
+ for (int i = 0 ; i < (TOTAL_RECEIVE_SIZE- 1 ) ; ++i)
167
157
check += buf[index + i];
168
- if (check % 256 != buf[index + 26 ])
158
+ if (check % 256 != buf[index + (TOTAL_RECEIVE_SIZE- 1 ) ])
169
159
{
170
- ROS_ERROR (" error 3! %d -- %d " ,check,buf[index +26 ]);
160
+ ROS_ERROR (" Error:Serial port verification failed! check: %d -- %d " ,check,buf[index +(TOTAL_RECEIVE_SIZE- 1 ) ]);
171
161
return false ;
172
162
}
173
163
@@ -183,6 +173,11 @@ bool MyAGV::readSpeed()
183
173
imu_data.angular_velocity .y = ((buf[index + 11 ] + buf[index + 12 ] * 256 ) - 10000 ) * 0.1 ;
184
174
imu_data.angular_velocity .z = ((buf[index + 13 ] + buf[index + 14 ] * 256 ) - 10000 ) * 0.1 ;
185
175
176
+ Battery_voltage = (float )buf[index + 16 ] / 10 .0f ;
177
+ Backup_Battery_voltage = (float )buf[index + 17 ] / 10 .0f ;
178
+
179
+ // ROS_INFO("Battery_voltage:%f",Battery_voltage);
180
+
186
181
// std::cout << "Received message is: " << "|" << vx << "," << vy << "," << vtheta << "|"
187
182
// << imu_data.linear_acceleration.x << "," << imu_data.linear_acceleration.y << "," << imu_data.linear_acceleration.z << "|"
188
183
// << imu_data.angular_velocity.x << "," << imu_data.angular_velocity.y << "," << imu_data.angular_velocity.z << std::endl;
@@ -193,59 +188,6 @@ bool MyAGV::readSpeed()
193
188
194
189
void MyAGV::writeSpeed (double movex, double movey, double rot)
195
190
{
196
- if (movex == 10 && movey == 10 && rot == 10 )
197
- {
198
- char buf[7 ] = {0xfe , 0xfe ,0x01 ,0x01 ,0x01 ,0x03 };
199
- boost::asio::write (sp, boost::asio::buffer (buf));
200
- unsigned char buf_header[1 ] = {0 };
201
-
202
- size_t ret;
203
- boost::system ::error_code er2;
204
- bool header_found = false ;
205
- time_t now_t = time (NULL );
206
- while (true ) {
207
-
208
- ret = boost::asio::read (sp, boost::asio::buffer (buf_header), er2);
209
-
210
- if (ret != 1 ) {
211
- continue ;
212
- }
213
- if (buf_header[0 ] != header[0 ]) {
214
- continue ;
215
- }
216
- bool header_2_found = false ;
217
- while (!header_2_found) {
218
- ret = boost::asio::read (sp, boost::asio::buffer (buf_header), er2);
219
- if (ret != 1 ) {
220
- continue ;
221
- }
222
- if (buf_header[0 ] != header[0 ]) {
223
- continue ;
224
- }
225
- header_2_found = true ;
226
- }
227
- header_found = true ;
228
- ret = boost::asio::read (sp, boost::asio::buffer (buf_header), er2);
229
- if (buf_header[0 ] == 0x01 )
230
- {
231
- ret = boost::asio::read (sp, boost::asio::buffer (buf_header), er2);
232
- if (buf_header[0 ] == 0x01 )
233
- {
234
- ret = boost::asio::read (sp, boost::asio::buffer (buf_header), er2);
235
- std_msgs::Int8 msg;
236
- msg.data = (int )buf_header[0 ] / 10 ;
237
- ROS_INFO (" Voltage: %d" , msg.data );
238
- pub_v.publish (msg);
239
- break ;
240
- }
241
- }
242
- if (time (NULL ) - now_t > 3 )
243
- {
244
- ROS_ERROR (" Get Voltage timeout" );
245
- break ;
246
- }
247
- }
248
- }else {
249
191
if (movex > 1.0 ) movex = 1.0 ;
250
192
if (movex < -1.0 ) movex = -1.0 ;
251
193
if (movey > 1.0 ) movey = 1.0 ;
@@ -265,8 +207,8 @@ void MyAGV::writeSpeed(double movex, double movey, double rot)
265
207
buf[3 ] = y_send;
266
208
buf[4 ] = rot_send;
267
209
buf[5 ] = check;
268
-
269
- boost::asio::write (sp, boost::asio::buffer (buf));}
210
+
211
+ boost::asio::write (sp, boost::asio::buffer (buf));
270
212
}
271
213
272
214
float MyAGV::invSqrt (float number)
@@ -375,6 +317,13 @@ void MyAGV::MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay
375
317
imu_data.orientation .z = q3;
376
318
}
377
319
320
+ void MyAGV::Publish_Voltage ()
321
+ {
322
+ std_msgs::Float32 voltage_msg;
323
+ voltage_msg.data = Battery_voltage;
324
+ pub_voltage.publish (voltage_msg);
325
+ }
326
+
378
327
void MyAGV::publisherImuSensor ()
379
328
{
380
329
sensor_msgs::Imu ImuSensor;
@@ -497,6 +446,8 @@ void MyAGV::execute(double linearX, double linearY, double angularZ)
497
446
publisherOdom ();
498
447
publisherImuSensor ();
499
448
// publisherImuSensorRaw();
449
+ Publish_Voltage ();
450
+
500
451
}
501
452
}
502
453
lastTime = currentTime;
0 commit comments