2 #include <ros/subscriber.h>
3 #include <ros/publisher.h>
5 #include <igvc_msgs/velocity_pair.h>
7 #include <std_msgs/Bool.h>
15 void cmdCallback(
const igvc_msgs::velocity_pair::ConstPtr& msg)
25 int main(
int argc,
char** argv)
27 ros::init(argc, argv,
"motor_controller");
29 ros::NodeHandle
nhp(
"~");
31 ros::Subscriber cmd_sub = nh.subscribe(
"/motors", 1,
cmdCallback);
33 ros::Subscriber enabled_sub = nh.subscribe(
"/robot_enabled", 1,
enabledCallback);
35 ros::Publisher enc_pub = nh.advertise<igvc_msgs::velocity_pair>(
"/encoders", 1000);
37 std::string device_path;
38 nhp.param(
string(
"device"), device_path,
string(
"/dev/igvc_motor_arduino"));
41 nhp.param(
string(
"baud_rate"), baud_rate, 9600);
47 ROS_ERROR_STREAM(
"Motor Controller serial port failed to open.");
52 while(ros::ok() && line.compare(
"Ready"))
54 ROS_INFO_STREAM(
"Waiting for arduino. " << line);
59 ROS_INFO_STREAM(
"Motor Controller ready.");
62 while(ros::ok() && port.
isOpen())
72 string ret = port.
readln();
77 size_t dollar = ret.find(
'$');
78 size_t comma = ret.find(
',');
79 size_t end = ret.find(
'\n');
80 string leftStr = ret.substr(dollar+1, comma-dollar-1);
81 string rightStr = ret.substr(comma+1, end-comma-1);
82 igvc_msgs::velocity_pair enc_msg;
83 enc_msg.left_velocity = atof(leftStr.c_str());
84 enc_msg.right_velocity = atof(rightStr.c_str());
85 enc_pub.publish(enc_msg);
87 ROS_ERROR_STREAM(
"Empty return from arduino.\t" << ret);
89 }
catch (std::out_of_range) { }
igvc_msgs::velocity_pair current_motor_command
int main(int argc, char **argv)
std::string readln()
Reads bytes from the serial port until or is found.
bool isOpen()
Returns true if the serial port is connected and open.
void write(std::string msg)
Writes the given string to the serial port.
void cmdCallback(const igvc_msgs::velocity_pair::ConstPtr &msg)
void enabledCallback(const std_msgs::BoolConstPtr &msg)
Helper class to simplify interfacing with serial port hardware.