Skip to content

Commit f01dce9

Browse files
committed
Added battery voltage ros topic
1 parent 4738b67 commit f01dce9

File tree

2 files changed

+31
-78
lines changed

2 files changed

+31
-78
lines changed

myagv_odometry/include/myagv_odometry/myAGV.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44
#include <ros/ros.h>
55
#include <ros/time.h>
6-
#include <geometry_msgs/Twist.h>
6+
#include <sensor_msgs/Imu.h>
7+
#include "std_msgs/Float32.h"
78
#include <nav_msgs/Odometry.h>
9+
#include <geometry_msgs/Twist.h>
810
#include <geometry_msgs/TransformStamped.h>
911
#include <tf/transform_broadcaster.h>
1012
#include <boost/asio.hpp>
11-
#include <sensor_msgs/Imu.h>
12-
1313

14-
//#define sampleFreq 20.5f // sample frequency in Hz
14+
//#define sampleFreq 20.5f // sample frequency in Hz
1515
#define twoKpDef 1.0f // (2.0f * 0.5f) // 2 * proportional gain
1616
#define twoKiDef 0.0f // (2.0f * 0.0f) // 2 * integral gain
17-
17+
#define TOTAL_RECEIVE_SIZE 27 // 27 字节 //The length of the data sent by the esp32 //esp32发送过来的数据的长度
1818
#define OFFSET_COUNT 200
1919

2020
class MyAGV
@@ -30,6 +30,7 @@ class MyAGV
3030
void publisherOdom();
3131
void publisherImuSensor();
3232
void publisherImuSensorRaw();
33+
void Publish_Voltage();
3334

3435
private:
3536
bool readSpeed();
@@ -59,10 +60,11 @@ class MyAGV
5960
float Gyroscope_Ydata_Offset;
6061
float Gyroscope_Zdata_Offset;
6162
float sampleFreq;
63+
float Battery_voltage,Backup_Battery_voltage;
6264
unsigned short Offest_Count;
6365
sensor_msgs::Imu imu_data;
6466
ros::NodeHandle n;
65-
ros::Publisher pub_odom,pub_v,pub_imu,pub,pub_imu_raw;
67+
ros::Publisher pub_odom,pub_voltage,pub_imu,pub,pub_imu_raw;
6668
tf::TransformBroadcaster odomBroadcaster;
6769
};
6870

myagv_odometry/src/myAGV.cpp

+23-72
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#include <time.h>
55

66
#include "myagv_odometry/myAGV.h"
7-
#include "std_msgs/Int8.h"
87

98
const unsigned char header[2] = { 0xfe, 0xfe };
109

1110
boost::asio::io_service iosev;
11+
1212
boost::asio::serial_port sp(iosev, "/dev/ttyAMA2");
1313

1414
boost::array<double, 36> odom_pose_covariance = {
@@ -26,16 +26,6 @@ boost::array<double, 36> odom_twist_covariance = {
2626
0, 0, 0, 0, 1e6, 0,
2727
0, 0, 0, 0, 0, 1e-9} };
2828

29-
void send()
30-
{
31-
;
32-
}
33-
34-
void receive()
35-
{
36-
;
37-
}
38-
3929
MyAGV::MyAGV()
4030
{
4131
x = 0.0;
@@ -69,7 +59,7 @@ bool MyAGV::init()
6959
pub_imu = n.advertise<sensor_msgs::Imu>("imu_data", 20);
7060
pub_imu_raw = n.advertise<sensor_msgs::Imu>("imu_raw_data", 20);
7161
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);
7363
restore(); //first restore,abort current err,don't restore
7464
return true;
7565
}
@@ -116,7 +106,7 @@ bool MyAGV::readSpeed()
116106
int i, length = 0, count = 0;
117107
unsigned char checkSum;
118108
unsigned char buf_header[1] = {0};
119-
unsigned char buf[27] = {0};
109+
unsigned char buf[TOTAL_RECEIVE_SIZE] = {0};
120110

121111
size_t ret;
122112
boost::system::error_code er2;
@@ -156,18 +146,18 @@ bool MyAGV::readSpeed()
156146
restoreRun();
157147
return false;
158148
}
159-
if (ret != 27) {
149+
if (ret != TOTAL_RECEIVE_SIZE) {
160150
ROS_ERROR("Read error %zu",ret);
161151
return false;
162152
}
163153

164154
int index = 0;
165155
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)
167157
check += buf[index + i];
168-
if (check % 256 != buf[index + 26])
158+
if (check % 256 != buf[index + (TOTAL_RECEIVE_SIZE-1)])
169159
{
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)]);
171161
return false;
172162
}
173163

@@ -183,6 +173,11 @@ bool MyAGV::readSpeed()
183173
imu_data.angular_velocity.y = ((buf[index + 11] + buf[index + 12] * 256 ) - 10000) * 0.1;
184174
imu_data.angular_velocity.z = ((buf[index + 13] + buf[index + 14] * 256 ) - 10000) * 0.1;
185175

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+
186181
//std::cout << "Received message is: " << "|" << vx << "," << vy << "," << vtheta << "|"
187182
// << imu_data.linear_acceleration.x << "," << imu_data.linear_acceleration.y << "," << imu_data.linear_acceleration.z << "|"
188183
// << imu_data.angular_velocity.x << "," << imu_data.angular_velocity.y << "," << imu_data.angular_velocity.z << std::endl;
@@ -193,59 +188,6 @@ bool MyAGV::readSpeed()
193188

194189
void MyAGV::writeSpeed(double movex, double movey, double rot)
195190
{
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{
249191
if (movex > 1.0) movex = 1.0;
250192
if (movex < -1.0) movex = -1.0;
251193
if (movey > 1.0) movey = 1.0;
@@ -265,8 +207,8 @@ void MyAGV::writeSpeed(double movex, double movey, double rot)
265207
buf[3] = y_send;
266208
buf[4] = rot_send;
267209
buf[5] = check;
268-
269-
boost::asio::write(sp, boost::asio::buffer(buf));}
210+
211+
boost::asio::write(sp, boost::asio::buffer(buf));
270212
}
271213

272214
float MyAGV::invSqrt(float number)
@@ -375,6 +317,13 @@ void MyAGV::MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay
375317
imu_data.orientation.z = q3;
376318
}
377319

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+
378327
void MyAGV::publisherImuSensor()
379328
{
380329
sensor_msgs::Imu ImuSensor;
@@ -497,6 +446,8 @@ void MyAGV::execute(double linearX, double linearY, double angularZ)
497446
publisherOdom();
498447
publisherImuSensor();
499448
//publisherImuSensorRaw();
449+
Publish_Voltage();
450+
500451
}
501452
}
502453
lastTime = currentTime;

0 commit comments

Comments
 (0)