DiffDriverController.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * The MIT License (MIT)
4 *
5 * Copyright (c) 2018 Bluewhale Robot
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Author: Xie fusheng
26 * Randoms
27 *******************************************************************************/
28 
29 #include <time.h>
31 
32 namespace xiaoqiang_driver
33 {
35 {
36  max_wheelspeed = 2.0;
37  cmd_topic = "cmd_vel";
38  xq_status = new StatusPublisher();
39  cmd_serial = NULL;
40  MoveFlag = true;
41 }
42 
43 DiffDriverController::DiffDriverController(double max_speed_, std::string cmd_topic_, StatusPublisher* xq_status_,
44  CallbackAsyncSerial* cmd_serial_)
45 {
46  MoveFlag = true;
47  max_wheelspeed = max_speed_;
48  cmd_topic = cmd_topic_;
49  xq_status = xq_status_;
50  cmd_serial = cmd_serial_;
51 }
52 
54 {
55  ros::NodeHandle nodeHandler;
57  ros::Subscriber sub2 = nodeHandler.subscribe("imu_cal", 1, &DiffDriverController::imuCalibration, this);
58  ros::Subscriber sub3 = nodeHandler.subscribe("global_move_flag", 1, &DiffDriverController::updateMoveFlag, this);
59  ros::Subscriber sub4 = nodeHandler.subscribe("infrared_sensor", 1, &DiffDriverController::updateBarDetectFlag, this);
60  ros::spin();
61 }
62 void DiffDriverController::updateMoveFlag(const std_msgs::Bool& moveFlag)
63 {
64  boost::mutex::scoped_lock lock(mMutex);
65  MoveFlag = moveFlag.data;
66 }
67 void DiffDriverController::imuCalibration(const std_msgs::Bool& calFlag)
68 {
69  if (calFlag.data)
70  {
71  //下发底层imu标定命令
72  char cmd_str[5] = { (char)0xcd, (char)0xeb, (char)0xd7, (char)0x01, (char)0x43 };
73  if (NULL != cmd_serial)
74  {
75  cmd_serial->write(cmd_str, 5);
76  }
77  }
78 }
79 void DiffDriverController::updateBarDetectFlag(const std_msgs::Bool& DetectFlag)
80 {
81  if (DetectFlag.data)
82  {
83  //下发底层红外开启命令
84  char cmd_str[6] = { (char)0xcd, (char)0xeb, (char)0xd7, (char)0x02, (char)0x44, (char)0x01 };
85  if (NULL != cmd_serial)
86  {
87  cmd_serial->write(cmd_str, 6);
88  }
89  }
90  else
91  {
92  //下发底层红外禁用命令
93  char cmd_str[6] = { (char)0xcd, (char)0xeb, (char)0xd7, (char)0x02, (char)0x44, (char)0x00 };
94  if (NULL != cmd_serial)
95  {
96  cmd_serial->write(cmd_str, 6);
97  }
98  }
99 }
100 void DiffDriverController::sendcmd(const geometry_msgs::Twist& command)
101 {
102  static time_t t1 = time(NULL), t2;
103  int i = 0, wheel_ppr = 1;
104  double separation = 0, radius = 0, speed_lin = 0, speed_ang = 0, speed_temp[2];
105  char speed[2] = { 0, 0 }; //右一左二
106  char cmd_str[13] = { (char)0xcd, (char)0xeb, (char)0xd7, (char)0x09, (char)0x74, (char)0x53, (char)0x53,
107  (char)0x53, (char)0x53, (char)0x00, (char)0x00, (char)0x00, (char)0x00 };
108 
109  if (xq_status->get_status() == 0)
110  return; //底层还在初始化
111  separation = xq_status->get_wheel_separation();
112  radius = xq_status->get_wheel_radius();
113  wheel_ppr = xq_status->get_wheel_ppr();
114  //转换速度单位,由米转换成转
115  speed_lin = command.linear.x / (2.0 * PI * radius);
116  speed_ang = command.angular.z * separation / (2.0 * PI * radius);
117 
118  float scale = std::max(std::abs(speed_lin + speed_ang / 2.0), std::abs(speed_lin - speed_ang / 2.0)) / max_wheelspeed;
119  if (scale > 1.0)
120  {
121  scale = 1.0 / scale;
122  }
123  else
124  {
125  scale = 1.0;
126  }
127  //转出最大速度百分比,并进行限幅
128  speed_temp[0] = scale * (speed_lin + speed_ang / 2) / max_wheelspeed * 100.0;
129  speed_temp[0] = std::min(speed_temp[0], 100.0);
130  speed_temp[0] = std::max(-100.0, speed_temp[0]);
131 
132  speed_temp[1] = scale * (speed_lin - speed_ang / 2) / max_wheelspeed * 100.0;
133  speed_temp[1] = std::min(speed_temp[1], 100.0);
134  speed_temp[1] = std::max(-100.0, speed_temp[1]);
135 
136  // std::cout<<"radius "<<radius<<std::endl;
137  // std::cout<<"ppr "<<wheel_ppr<<std::endl;
138  // std::cout<<"pwm "<<speed_temp[0]<<std::endl;
139  // command.linear.x/
140  for (i = 0; i < 2; i++)
141  {
142  speed[i] = speed_temp[i];
143  if (speed[i] < 0)
144  {
145  cmd_str[5 + i] = (char)0x42; // B
146  cmd_str[9 + i] = -speed[i];
147  }
148  else if (speed[i] > 0)
149  {
150  cmd_str[5 + i] = (char)0x46; // F
151  cmd_str[9 + i] = speed[i];
152  }
153  else
154  {
155  cmd_str[5 + i] = (char)0x53; // S
156  cmd_str[9 + i] = (char)0x00;
157  }
158  }
159 
160  // std::cout<<"distance1 "<<xq_status->car_status.distance1<<std::endl;
161  // std::cout<<"distance2 "<<xq_status->car_status.distance2<<std::endl;
162  // std::cout<<"distance3 "<<xq_status->car_status.distance3<<std::endl;
163  // if(xq_status->get_status()==2)
164  // {
165  // //有障碍物
166  // if(xq_status->car_status.distance1<30&&xq_status->car_status.distance1>0&&cmd_str[6]==(char)0x46)
167  // {
168  // cmd_str[6]=(char)0x53;
169  // }
170  // if(xq_status->car_status.distance2<30&&xq_status->car_status.distance2>0&&cmd_str[5]==(char)0x46)
171  // {
172  // cmd_str[5]=(char)0x53;
173  // }
174  // if(xq_status->car_status.distance3<20&&xq_status->car_status.distance3>0&&(cmd_str[5]==(char)0x42||cmd_str[6]==(char)0x42))
175  // {
176  // cmd_str[5]=(char)0x53;
177  // cmd_str[6]=(char)0x53;
178  // }
179  // if(xq_status->car_status.distance1<15&&xq_status->car_status.distance1>0&&(cmd_str[5]==(char)0x46||cmd_str[6]==(char)0x46))
180  // {
181  // cmd_str[5]=(char)0x53;
182  // cmd_str[6]=(char)0x53;
183  // }
184  // if(xq_status->car_status.distance2<15&&xq_status->car_status.distance2>0&&(cmd_str[5]==(char)0x46||cmd_str[6]==(char)0x46))
185  // {
186  // cmd_str[5]=(char)0x53;
187  // cmd_str[6]=(char)0x53;
188  // }
189  // }
190 
191  boost::mutex::scoped_lock lock(mMutex);
192  if (!MoveFlag)
193  {
194  cmd_str[5] = (char)0x53;
195  cmd_str[6] = (char)0x53;
196  }
197  if (NULL != cmd_serial)
198  {
199  cmd_serial->write(cmd_str, 13);
200  }
201 
202  // command.linear.x
203 }
204 }
void updateMoveFlag(const std_msgs::Bool &moveFlag)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void spin(Spinner &spinner)
#define PI
void sendcmd(const geometry_msgs::Twist &command)
void write(const char *data, size_t size)
void updateBarDetectFlag(const std_msgs::Bool &DetectFlag)
void imuCalibration(const std_msgs::Bool &calFlag)


xiaoqiang_driver
Author(s): Xie fusheng
autogenerated on Mon Jun 10 2019 15:53:12