Main Page
Namespaces
Classes
Files
File List
File Members
src
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
>
34
#include "
xiaoqiang_driver/DiffDriverController.h
"
35
#include "
xiaoqiang_driver/StatusPublisher.h
"
36
#include "
xiaoqiang_driver/AsyncSerial.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
}
xiaoqiang_driver::DiffDriverController::run
void run()
Definition:
DiffDriverController.cpp:53
AsyncSerial::errorStatus
bool errorStatus() const
Definition:
AsyncSerial.cpp:121
CallbackAsyncSerial
Definition:
AsyncSerial.h:177
ros::start
ROSCPP_DECL void start()
main
int main(int argc, char **argv)
Definition:
main.cpp:40
DiffDriverController.h
ros::init
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std
ros::Rate
AsyncSerial.h
StatusPublisher.h
CallbackAsyncSerial::setCallback
void setCallback(const boost::function< void(const char *, size_t)> &callback)
Definition:
AsyncSerial.cpp:596
ros::ok
ROSCPP_DECL bool ok()
xiaoqiang_driver::StatusPublisher
Definition:
StatusPublisher.h:69
ROS_DEBUG_STREAM
#define ROS_DEBUG_STREAM(args)
AsyncSerial::write
void write(const char *data, size_t size)
Definition:
AsyncSerial.cpp:142
ros::Rate::sleep
bool sleep()
ros.h
package.h
AsyncSerial::close
void close()
Definition:
AsyncSerial.cpp:127
xiaoqiang_driver::DiffDriverController
Definition:
DiffDriverController.h:38
console.h
ros::shutdown
ROSCPP_DECL void shutdown()
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
AsyncSerial::isOpen
bool isOpen() const
Definition:
AsyncSerial.cpp:116
xiaoqiang_driver::StatusPublisher::Update
void Update(const char *data, unsigned int len)
Definition:
StatusPublisher.cpp:86
ROS_DEBUG
#define ROS_DEBUG(...)
xiaoqiang_driver
Author(s): Xie fusheng
autogenerated on Mon Jun 10 2019 15:53:12