arduino_comms.cpp
Go to the documentation of this file.
2 #include <ros/console.h>
3 #include <sstream>
4 #include <cstdlib>
5 
7 {
8  std::string response = sendMsg("\r");
9 }
10 
11 void ArduinoComms::readEncoderValues(int &val_1, int &val_2)
12 {
13  std::string response = sendMsg("e\r");
14 
15  std::string delimiter = " ";
16  size_t del_pos = response.find(delimiter);
17  std::string token_1 = response.substr(0, del_pos);
18  std::string token_2 = response.substr(del_pos + delimiter.length());
19 
20  val_1 = std::atoi(token_1.c_str());
21  val_2 = std::atoi(token_2.c_str());
22 }
23 
24 void ArduinoComms::setMotorValues(int val_1, int val_2)
25 {
26  std::stringstream ss;
27  ss << "m " << val_1 << " " << val_2 << "\r";
28  sendMsg(ss.str(), false);
29 }
30 
31 void ArduinoComms::setPidValues(float k_p, float k_d, float k_i, float k_o)
32 {
33  std::stringstream ss;
34  ss << "u " << k_p << ":" << k_d << ":" << k_i << ":" << k_o << "\r";
35  sendMsg(ss.str());
36 }
37 
38 std::string ArduinoComms::sendMsg(const std::string &msg_to_send, bool print_output)
39 {
40  serial_conn_.write(msg_to_send);
41  std::string response = serial_conn_.readline();
42 
43  if (print_output)
44  {
45  ROS_INFO_STREAM("Sent: " << msg_to_send);
46  ROS_INFO_STREAM("Received: " << response);
47  }
48 
49  return response;
50 }
void setMotorValues(int val_1, int val_2)
serial::Serial serial_conn_
Underlying serial connection.
Definition: arduino_comms.h:28
void readEncoderValues(int &val_1, int &val_2)
std::string sendMsg(const std::string &msg_to_send, bool print_output=false)
#define ROS_INFO_STREAM(args)
void sendEmptyMsg()
const std::string response
void setPidValues(float k_p, float k_d, float k_i, float k_o)


diffdrive_arduino
Author(s): Josh Newans
autogenerated on Mon Feb 28 2022 22:13:39