Tetrapod Project
teensy_serial.cpp
Go to the documentation of this file.
1 #include "teensy_serial.h"
2 
3 TeensySerial::TeensySerial(int _number_of_motors)
4 {
5  this->number_of_motors = _number_of_motors;
6 
7  // Initialize the size of the transmitt buffer
8  // 3 values per motor (joint position, joint velocity, joint torque)
9  // 8 bytes per value (per double)
10  this->tx_buffer_size = number_of_motors * 8 * 3;
11 
12  // Initialize the size of the receive buffer
13  // 1 command + 1 value per motor
14  // 8 bytes per value (double)
15  this->rx_buffer_size = (1 + number_of_motors) * 8;
16 
17  // Allocate memory for the receive buffer
18  this->rx_buffer = new unsigned char[rx_buffer_size];
19 
20  // Allocate memory for the transmitt buffer
21  this->tx_buffer = new unsigned char[tx_buffer_size];
22 }
23 
25 {
26  // Free the memory of the receive buffer
27  delete rx_buffer;
28 
29  // Free the memory of the transmitt buffer
30  delete tx_buffer;
31 }
32 
34 {
35  // Check if any data is available on the serial line
36  if(Serial.available())
37  {
38  // Return true if there is any data available
39  return true;
40  }
41  else
42  {
43  // Return false if there is no data available
44  return false;
45  }
46 }
47 
48 void TeensySerial::receiveControlCommands(double &_control_command_type, double* _control_commands)
49 {
50  int i = 0;
51 
52  // Read data from the serial line as long as there is data available.
53  // Up to 64 bytes can be read at a time
54  while(Serial.available())
55  {
56  // If several messages are waiting on the line, we only care about the latest.
57  // We therefore overwrite the previously received data
58  if(i >= rx_buffer_size)
59  {
60  i = 0;
61  }
62 
63  // Store the data in the receive buffer
64  this->rx_buffer[i] = Serial.read();
65  i++;
66  }
67 
68  // Create a temporary variable to store the control id of the incomming control message
69  double temp_id;
70 
71  // Fetch the control ID from the incomming message
72  temp_id = *((double*)this->rx_buffer);
73 
74  // Store the control command type
75  _control_command_type = temp_id;
76 
77  // Go through the control commands for the different motors
78  for(int i = 1; i < this->number_of_motors + 1; i++)
79  {
80  // Temporary variable for storage
81  double val;
82 
83  // Assign the control value to the temporary variable
84  val = *((double*)this->rx_buffer + i);
85 
86  // Store the control value for motor i
87  _control_commands[i - 1] = val;
88  }
89 
90 }
91 
92 void TeensySerial::sendStates(double *joint_positions, double *joint_velocities, double *joint_torques)
93 {
94  // Put the joint position values in the transmitt buffer
95  for(int i = 0; i < this->number_of_motors*8; i++)
96  {
97  this->tx_buffer[i] = ((char *) joint_positions)[i];
98  }
99 
100  int offset = 8*number_of_motors;
101 
102  // Put the joint velocities in the transmitt buffer
103  for(int i = 0; i < this->number_of_motors*8; i++)
104  {
105  this->tx_buffer[i + offset] = ((char *) joint_velocities)[i];
106  }
107 
108  offset = 8*2*number_of_motors;
109 
110  // Put the joint torques in the transmitt buffer
111  for(int i = 0; i < this->number_of_motors*8; i++)
112  {
113  this->tx_buffer[i + offset] = ((char *) joint_torques)[i];
114  }
115 
116  // Send the state data
117  Serial.write(tx_buffer, tx_buffer_size);
118 }
119 
121 {
122  for(int i = 0; i < this->number_of_motors*3; i++)
123  {
124  double val;
125 
126  val = *((double*)this->tx_buffer + i);
127 
128  Serial.println(val);
129  }
130 }
void receiveControlCommands(double &_control_command_type, double *_control_commands)
Reads control command data available on the serial command and stores it.
unsigned char * tx_buffer
Transmit buffer.
Definition: teensy_serial.h:45
void sendStates(double *_joint_positions, double *_joint_velocities, double *_joint_torques)
Sends the joint states to the serial line.
int tx_buffer_size
Transmit buffer size.
Definition: teensy_serial.h:36
bool areControlCommandsAvailable()
Checks if there is any available data on the serial port.
int rx_buffer_size
Receive buffer size.
Definition: teensy_serial.h:39
int number_of_motors
Number of motors.
Definition: teensy_serial.h:33
void printTxBuffer()
TeensySerial(int _number_of_motors)
unsigned char * rx_buffer
Receive buffer.
Definition: teensy_serial.h:42