motor_controller/main.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <ros/subscriber.h>
3 #include <ros/publisher.h>
4 #include <string>
5 #include <igvc_msgs/velocity_pair.h>
6 #include <igvc/SerialPort.h>
7 #include <std_msgs/Bool.h>
8 
9 using namespace std;
10 
11 igvc_msgs::velocity_pair current_motor_command;
12 
13 bool enabled = false;
14 
15 void cmdCallback(const igvc_msgs::velocity_pair::ConstPtr& msg)
16 {
17  current_motor_command = *msg;
18 }
19 
20 void enabledCallback(const std_msgs::BoolConstPtr& msg)
21 {
22  enabled = msg->data;
23 }
24 
25 int main(int argc, char** argv)
26 {
27  ros::init(argc, argv, "motor_controller");
28  ros::NodeHandle nh;
29  ros::NodeHandle nhp("~");
30 
31  ros::Subscriber cmd_sub = nh.subscribe("/motors", 1, cmdCallback);
32 
33  ros::Subscriber enabled_sub = nh.subscribe("/robot_enabled", 1, enabledCallback);
34 
35  ros::Publisher enc_pub = nh.advertise<igvc_msgs::velocity_pair>("/encoders", 1000);
36 
37  std::string device_path;
38  nhp.param(string("device"), device_path, string("/dev/igvc_motor_arduino"));
39 
40  int baud_rate;
41  nhp.param(string("baud_rate"), baud_rate, 9600);
42 
43  SerialPort port(device_path, baud_rate);
44 
45  if(!port.isOpen())
46  {
47  ROS_ERROR_STREAM("Motor Controller serial port failed to open.");
48  return -1;
49  }
50 
51  auto line = port.readln();
52  while(ros::ok() && line.compare("Ready"))
53  {
54  ROS_INFO_STREAM("Waiting for arduino. " << line);
55  line = port.readln();
56  usleep(250);
57  }
58 
59  ROS_INFO_STREAM("Motor Controller ready.");
60 
61  ros::Rate rate(10);
62  while(ros::ok() && port.isOpen())
63  {
64  ros::spinOnce();
65 
66  string msg = "$" + to_string(enabled?current_motor_command.left_velocity:0.0) + "," + to_string(enabled?current_motor_command.right_velocity:0.0) + "\n";
67 
68  ROS_INFO_STREAM(msg);
69 
70  port.write(msg);
71 
72  string ret = port.readln();
73 
74  try {
75  if(!ret.empty())
76  {
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);
86  } else {
87  ROS_ERROR_STREAM("Empty return from arduino.\t" << ret);
88  }
89  } catch (std::out_of_range) { }
90 
91  rate.sleep();
92  }
93 }
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.
Definition: SerialPort.cpp:84
bool enabled
bool isOpen()
Returns true if the serial port is connected and open.
Definition: SerialPort.cpp:36
void write(std::string msg)
Writes the given string to the serial port.
void cmdCallback(const igvc_msgs::velocity_pair::ConstPtr &msg)
ros::NodeHandle * nhp
void enabledCallback(const std_msgs::BoolConstPtr &msg)
Helper class to simplify interfacing with serial port hardware.
Definition: SerialPort.h:14


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45