main.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 <iostream>
30 #include <boost/thread.hpp>
31 #include <ros/ros.h>
32 #include <ros/console.h>
33 #include <ros/package.h>
37 
38 using namespace std;
39 
40 int main(int argc, char** argv)
41 {
42  ROS_DEBUG("welcome to xiaoqiang serial server,please feel free at home!");
43  ros::init(argc, argv, "xiaoqiang_driver");
44  ros::start();
45 
46  //获取串口参数
47  std::string port;
48  ros::param::param<std::string>("~port", port, "/dev/xiaoqiang");
49  int baud;
50  ros::param::param<int>("~baud", baud, 115200);
51  ROS_DEBUG_STREAM("port:" << port << " baud:" << baud);
52 
53  //获取小车机械参数
54  double separation = 0, radius = 0;
55  bool DebugFlag = false;
56  ros::param::param<double>("~wheel_separation", separation, 0.37);
57  ros::param::param<double>("~wheel_radius", radius, 0.0625);
58  ros::param::param<bool>("~debug_flag", DebugFlag, false);
59  xiaoqiang_driver::StatusPublisher xq_status(separation, radius);
60 
61  //获取小车控制参数
62  double max_speed;
63  string cmd_topic;
64  ros::param::param<double>("~max_speed", max_speed, 2.0);
65  ros::param::param<std::string>("~cmd_topic", cmd_topic, "cmd_vel");
66 
67  try
68  {
69  CallbackAsyncSerial serial(port, baud);
70  serial.setCallback(boost::bind(&xiaoqiang_driver::StatusPublisher::Update, &xq_status, _1, _2));
71  xiaoqiang_driver::DiffDriverController xq_diffdriver(max_speed, cmd_topic, &xq_status, &serial);
72  boost::thread cmd2serialThread(&xiaoqiang_driver::DiffDriverController::run, &xq_diffdriver);
73  // send test flag
74  char debugFlagCmd[] = { (char)0xcd, (char)0xeb, (char)0xd7, (char)0x01, 'T' };
75  if (DebugFlag)
76  {
77  ROS_DEBUG_STREAM("Debug mode Enabled");
78  serial.write(debugFlagCmd, 5);
79  }
80  // send reset cmd
81  char resetCmd[] = { (char)0xcd, (char)0xeb, (char)0xd7, (char)0x01, 'I' };
82  serial.write(resetCmd, 5);
83 
84  ros::Rate r(50); //发布周期为50hz
85  while (ros::ok())
86  {
87  if (serial.errorStatus() || serial.isOpen() == false)
88  {
89  ROS_ERROR_STREAM("Error: serial port closed unexpectedly");
90  break;
91  }
92  xq_status.Refresh(); //定时发布状态
93  r.sleep();
94  // cout<<"run"<<endl;
95  }
96 
97  quit:
98  serial.close();
99  }
100  catch (std::exception& e)
101  {
102  ROS_ERROR_STREAM("Open " << port << " failed.");
103  ROS_ERROR_STREAM("Exception: " << e.what());
104  }
105 
106  ros::shutdown();
107  return 0;
108 }
bool errorStatus() const
ROSCPP_DECL void start()
int main(int argc, char **argv)
Definition: main.cpp:40
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void setCallback(const boost::function< void(const char *, size_t)> &callback)
ROSCPP_DECL bool ok()
#define ROS_DEBUG_STREAM(args)
void write(const char *data, size_t size)
bool sleep()
ROSCPP_DECL void shutdown()
#define ROS_ERROR_STREAM(args)
bool isOpen() const
void Update(const char *data, unsigned int len)
#define ROS_DEBUG(...)


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