tmcl_interpreter.cpp
Go to the documentation of this file.
1 
6 #include <ros/console.h>
7 #include "tmcl_interpreter.h"
8 
10 
11 /* Constructor */
12 TmclInterpreter::TmclInterpreter(uint16_t timeout_ms, uint8_t comm_exec_cmd_retries,
13  std::vector<std::string> param_ap_name, std::vector<int> param_ap_type) :
14  timeout_ms_(timeout_ms),
15  comm_exec_cmd_retries_(comm_exec_cmd_retries),
16  param_ap_name_(param_ap_name),
17  param_ap_type_(param_ap_type)
18 {
19  ROS_DEBUG_STREAM("[TmclInterpreter::" << __func__ << "] called");
20 
22 
24  {
25  tmcl_cfg_.p_socket_can = nullptr;
26  tmcl_cfg_.interface_name = "can0";
27  tmcl_cfg_.tx_id = 1;
28  tmcl_cfg_.rx_id = 2;
29  }
30  interface_enabled_ = false;
31  b_retries_exceeded_ = false;
32 }
33 
34 /* Destructor */
36 {
37  ROS_DEBUG_STREAM("[TmclInterpreter::" << __func__ << "] called");
38 
40  {
42  tmcl_cfg_.tx_id = 0;
43  tmcl_cfg_.rx_id = 0;
44  }
45  interface_enabled_ = false;
46 }
47 
48 /* Reset interface */
50 {
51  ROS_INFO_STREAM("[TmclInterpreter::" << __func__ << "] called");
52 
53  bool b_result = false;
54 
56  {
58 
60  {
61  ROS_INFO_STREAM("[" << __func__ <<"] called");
62  interface_enabled_ = true;
63  b_result = true;
64  }
65  }
66  else
67  {
68  ROS_ERROR_STREAM("[" << __func__ <<"] Interface not yet supported");
69  }
70 
71  return b_result;
72 }
73 
74 /* Execute a TMCL command within the program */
75 bool TmclInterpreter::executeCmd(tmcl_cmd_t cmd, uint8_t type, uint8_t motor, int32_t *val)
76 {
77  ROS_DEBUG_STREAM("[" << __func__ << "] called");
78 
79  bool b_result = false;
80  uint8_t n_retries = comm_exec_cmd_retries_;
81  uint8_t rx_msg[TMCL_MSG_SZ] = {0};
82  uint32_t rx_msg_id = 0;
83  uint8_t rx_msg_sz = 0;
84 
85  tmcl_msg_t tmcl_msg;
86 
88  {
90  {
91  tmcl_msg.tx_id = tmcl_cfg_.tx_id;
92  tmcl_msg.rx_id = tmcl_cfg_.rx_id;
93  tmcl_msg.cmd = cmd;
94  tmcl_msg.type = type;
95  tmcl_msg.motor = motor;
96  tmcl_msg.value[0] = (*val & 0xFF000000) >> 24;
97  tmcl_msg.value[1] = (*val & 0x00FF0000) >> 16;
98  tmcl_msg.value[2] = (*val & 0x0000FF00) >> 8;
99  tmcl_msg.value[3] = (*val & 0x000000FF);
100  // Setting cmd, type, motor, value is always needed every call to execute_cmd()
101  uint8_t tx_msg[TMCL_MSG_SZ] = {tmcl_msg.cmd, tmcl_msg.type, tmcl_msg.motor, tmcl_msg.value[0],
102  tmcl_msg.value[1], tmcl_msg.value[2], tmcl_msg.value[3]};
103  auto start_time = std::chrono::system_clock::now();
104  auto end_time = start_time;
105 
106  while (0 < n_retries)
107  {
108  ROS_DEBUG("[%s] [T%d] before execute_cmd(), value=%d sending "
109  "[0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x]", __func__, n_retries, *val, tmcl_msg.tx_id,
110  tmcl_msg.cmd, tmcl_msg.type, tmcl_msg.motor, tmcl_msg.value[0], tmcl_msg.value[1], tmcl_msg.value[2],
111  tmcl_msg.value[3]);
112 
113  /* Send TMCL command */
114  if(tmcl_cfg_.p_socket_can->writeFrame(tmcl_msg.tx_id, tx_msg, TMCL_MSG_SZ))
115  {
116  while(timeout_ms_ > std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count())
117  {
119  tmcl_cfg_.p_socket_can->readFrame(&rx_msg_id, rx_msg, &rx_msg_sz))
120  {
121  /* A response for the TMCL command is received */
122  if((rx_msg_id == tmcl_msg.rx_id) &&
123  (rx_msg_sz == TMCL_MSG_SZ) &&
124  (rx_msg[0] == tmcl_msg.tx_id) &&
125  (rx_msg[2] == tmcl_msg.cmd))
126  {
127  tmcl_msg.sts = (tmcl_sts_t)rx_msg[1];
128  tmcl_msg.value[0] = rx_msg[3];
129  tmcl_msg.value[1] = rx_msg[4];
130  tmcl_msg.value[2] = rx_msg[5];
131  tmcl_msg.value[3] = rx_msg[6];
132 
133  //Rx: [Reply Address] | <Module Address> | <Status> | <Command> | <Value> | <Value> | <Value> | <Value> | [Checksum]
134  ROS_DEBUG("\n[%s] [T%d] execute_cmd() success, received "
135  "[0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x]\n", __func__, n_retries, tmcl_msg.tx_id,
136  tmcl_msg.sts, tmcl_msg.cmd, tmcl_msg.value[0], tmcl_msg.value[1], tmcl_msg.value[2],
137  tmcl_msg.value[3]);
138  *val = (tmcl_msg.value[0] << 24) + (tmcl_msg.value[1] << 16) + (tmcl_msg.value[2] << 8) +
139  tmcl_msg.value[3];
140  b_result = true;
141  break;
142  }
143  }
144  end_time = std::chrono::system_clock::now();
145  }
146  }
147 
148  if(!b_result)
149  {
150  n_retries--;
151  }
152  else
153  {
154  break;
155  }
156  }
157 
158  if(n_retries == 0)
159  {
160  b_retries_exceeded_ = true;
161  ROS_ERROR_STREAM("[" << __func__ << "] Retries exceeded");
162  }
163  }
164  else
165  {
166  ROS_ERROR_STREAM("[" << __func__ << "] Interface not yet supported");
167  }
168  }
169 
170  return b_result;
171 }
172 
173 bool TmclInterpreter::executeCmd(tmcl_cmd_t cmd, std::string type, uint8_t motor, int32_t *val)
174 {
175  bool b_result = false;
176  uint8_t int_type = 0;
177  uint8_t index = 0;
178 
179  auto iterator = std::find(param_ap_name_.begin(), param_ap_name_.end(), type);
180 
181  if(iterator != param_ap_name_.end())
182  {
183  index = std::distance(param_ap_name_.begin(), iterator);
184  int_type = param_ap_type_[index];
185  b_result = executeCmd(cmd, int_type, motor, val);
186  }
187  else
188  {
189  ROS_DEBUG_STREAM("[" << __func__ << "] Instruction Type: " << type << " not found");
190  }
191 
192  return b_result;
193 }
194 
195 /* Shutdown interface */
197 {
198  bool b_result = false;
199 
200  ROS_INFO_STREAM("[TmclInterpreter::" << __func__ << "] called");
201 
203  {
205  delete tmcl_cfg_.p_socket_can;
206  tmcl_cfg_.p_socket_can = nullptr;
207  interface_enabled_ = false;
208  b_result = true;
209  }
210 
211  return b_result;
212 }
213 
214 /* Getter b_retries_exceeded_ variable */
216 {
217  return b_retries_exceeded_;
218 }
tmcl_msg_t::type
uint8_t type
Definition: tmcl_interpreter.h:91
TmclInterpreter::tmcl_interface_
tmcl_interface_t tmcl_interface_
Definition: tmcl_interpreter.h:123
tmcl_msg_t::tx_id
uint16_t tx_id
Definition: tmcl_interpreter.h:88
TmclInterpreter::~TmclInterpreter
~TmclInterpreter()
Definition: tmcl_interpreter.cpp:35
tmcl_msg_t::sts
tmcl_sts_t sts
Definition: tmcl_interpreter.h:94
TmclInterpreter::tmcl_cfg_
tmcl_cfg_t tmcl_cfg_
Definition: tmcl_interpreter.h:124
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
SocketCAN::initialize
bool initialize(const char *interface_name)
Definition: socket_can_wrapper.cpp:31
TmclInterpreter::executeCmd
bool executeCmd(tmcl_cmd_t cmd, uint8_t type, uint8_t motor, int32_t *val)
Definition: tmcl_interpreter.cpp:75
SocketCAN::framesAvailable
bool framesAvailable()
Definition: socket_can_wrapper.cpp:127
SocketCAN
Definition: socket_can_wrapper.h:9
TmclInterpreter::getRetriesExceededStatus
bool getRetriesExceededStatus()
Definition: tmcl_interpreter.cpp:215
TMCL_INTERFACE_CAN
@ TMCL_INTERFACE_CAN
Definition: tmcl_interpreter.h:37
TmclInterpreter::param_ap_name_
std::vector< std::string > param_ap_name_
Definition: tmcl_interpreter.h:130
ROS_DEBUG_STREAM
#define ROS_DEBUG_STREAM(args)
tmcl_msg_t::value
uint8_t value[TMCL_MSG_VALUE_SZ]
Definition: tmcl_interpreter.h:93
TmclInterpreter::comm_exec_cmd_retries_
uint8_t comm_exec_cmd_retries_
Definition: tmcl_interpreter.h:129
TmclInterpreter::TmclInterpreter
TmclInterpreter(uint16_t timeout_ms, uint8_t comm_exec_cmd_retries, std::vector< std::string > param_ap_name, std::vector< int > param_ap_type)
Definition: tmcl_interpreter.cpp:12
tmcl_cfg_t::interface_name
std::string interface_name
Definition: tmcl_interpreter.h:47
TmclInterpreter::timeout_ms_
uint16_t timeout_ms_
Definition: tmcl_interpreter.h:128
console.h
SocketCAN::deinitialize
void deinitialize()
Definition: socket_can_wrapper.cpp:109
TmclInterpreter::resetInterface
bool resetInterface()
Definition: tmcl_interpreter.cpp:49
TmclInterpreter::interface_enabled_
bool interface_enabled_
Definition: tmcl_interpreter.h:127
ROS_DEBUG
#define ROS_DEBUG(...)
tmcl_cfg_t::tx_id
uint16_t tx_id
Definition: tmcl_interpreter.h:48
tmcl_cfg_t::p_socket_can
SocketCAN * p_socket_can
Definition: tmcl_interpreter.h:46
SocketCAN::writeFrame
bool writeFrame(uint32_t id, uint8_t *data, uint8_t size)
Definition: socket_can_wrapper.cpp:196
tmcl_cmd_t
tmcl_cmd_t
Definition: tmcl_interpreter.h:57
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
SocketCAN::readFrame
bool readFrame(uint32_t *id, uint8_t *data, uint8_t *size)
Definition: socket_can_wrapper.cpp:162
tmcl_interpreter.h
TmclInterpreter::param_ap_type_
std::vector< int > param_ap_type_
Definition: tmcl_interpreter.h:131
tmcl_cfg_t::rx_id
uint16_t rx_id
Definition: tmcl_interpreter.h:49
tmcl_msg_t::rx_id
uint16_t rx_id
Definition: tmcl_interpreter.h:89
tmcl_msg_t::cmd
tmcl_cmd_t cmd
Definition: tmcl_interpreter.h:90
TmclInterpreter::shutdownInterface
bool shutdownInterface()
Definition: tmcl_interpreter.cpp:196
tmcl_msg_t
Definition: tmcl_interpreter.h:86
TmclInterpreter::b_retries_exceeded_
bool b_retries_exceeded_
Definition: tmcl_interpreter.h:133
tmcl_msg_t::motor
uint8_t motor
Definition: tmcl_interpreter.h:92
TMCL_MSG_SZ
const uint8_t TMCL_MSG_SZ
Definition: tmcl_interpreter.h:27
tmcl_sts_t
tmcl_sts_t
Definition: tmcl_interpreter.h:72


adi_tmcl
Author(s):
autogenerated on Wed Apr 2 2025 02:43:01