18 #ifndef HAND_BRIDGE_SDHX_H_ 19 #define HAND_BRIDGE_SDHX_H_ 22 #include "boost/format.hpp" 23 #include "boost/thread.hpp" 35 const size_t MAX_LINE = 1024;
37 char buffer[MAX_LINE+1] = {0};
39 int a = 0,
r = 1, offset = 0;
40 while(a >= 0 &&
r > 0){
41 if((a = serial.
waitData(boost::chrono::milliseconds(1))) > 0 && (
r = serial.
read(buffer+offset, MAX_LINE-offset)) > 0){
43 while(
char * extra = strchr(line,
'\n')){
53 offset = (buffer + offset +
r - line);
54 strncpy(buffer, line, offset);
57 std::cerr <<
"stopped reading" << std::endl;
58 boost::mutex::scoped_lock lock(data_mutex);
64 if(sscanf(line,
"rc=%hhx", &res) == 1){
66 boost::mutex::scoped_lock lock(data_mutex);
73 template<
typename T>
bool tryReadValues(
const char *line, T (&val)[2],
const char *format,
bool track_time =
false) {
75 if(sscanf(line, format, &val1, &val2) == 2){
76 boost::mutex::scoped_lock lock(data_mutex);
81 last_time = boost::chrono::steady_clock::now();
88 bool send(
const std::string &command){
89 boost::mutex::scoped_lock lock(send_mutex);
90 return serial.
write(command);
93 bool setPWM(
const int16_t &val,
const char *format) {
94 return val == 0 ||
send(boost::str(boost::format(format) % val));
99 SDHX() : rc(0), initialized(false), reading(false) {
102 if(initialized && read_thread.joinable()){
103 read_thread.interrupt();
107 bool init(
const char* port, int16_t min_pwm0, int16_t min_pwm1, int16_t max_pwm0, int16_t max_pwm1){
108 std::string dev(port);
109 if(!serial.
isOpen() && !serial.
init(&dev[0]))
return false;
111 boost::mutex::scoped_lock lock(data_mutex);
114 read_thread = boost::thread(boost::bind(&
SDHX::doRead,
this));
118 if(
setPWM(min_pwm0,
"set min_pwm0 %1%\r\n") &&
119 setPWM(min_pwm1,
"set min_pwm1 %1%\r\n") &&
120 setPWM(max_pwm0,
"set max_pwm0 %1%\r\n") &&
121 setPWM(max_pwm1,
"set max_pwm1 %1%\r\n")){
133 template <
typename Dur>
bool getData(int16_t (&p)[2], int16_t (&
v)[2], int16_t (&
c)[2],
const Dur &max_age){
134 boost::mutex::scoped_lock lock(data_mutex);
135 if((boost::chrono::steady_clock::now() - last_time) > max_age)
return false;
136 if(!reading)
return false;
148 return initialized &&
send(
"p;v;c\r\n");
151 return initialized &&
send(
"s\r\n");
153 bool move(
const int16_t (&p)[2],
const int16_t (&
v)[2],
const int16_t (&
c)[2]){
154 static boost::format
command(
"m %hd,%hd %hd,%hd %hd,%hd\r\n");
155 return initialized &&
send(boost::str(command % p[0] % p[1] %
v[0] %
v[1] %
c[0] %
c[1]));
163 boost::mutex::scoped_lock lock(data_mutex);
167 #endif // HAND_BRIDGE_SDHX_H_ bool write(uint8_t *data, int length)
bool setPWM(const int16_t &val, const char *format)
int read(char *buf, unsigned count)
bool move(const int16_t(&p)[2], const int16_t(&v)[2], const int16_t(&c)[2])
bool init(const char *port, int16_t min_pwm0, int16_t min_pwm1, int16_t max_pwm0, int16_t max_pwm1)
volatile bool initialized
int waitData(const Duration &duration)
bool getData(int16_t(&p)[2], int16_t(&v)[2], int16_t(&c)[2], const Dur &max_age)
bool tryReadValues(const char *line, T(&val)[2], const char *format, bool track_time=false)
boost::chrono::steady_clock::time_point last_time
ROSLIB_DECL std::string command(const std::string &cmd)
bool tryParseRC(const char *line)
bool send(const std::string &command)
boost::thread read_thread