command.cpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Includes
12 *****************************************************************************/
13 
14 #include "../../include/kobuki_driver/command.hpp"
15 
16 /*****************************************************************************
17 ** Namespaces
18 *****************************************************************************/
19 
20 namespace kobuki {
21 
22 /*****************************************************************************
23 ** Static variables initialization
24 *****************************************************************************/
25 
26 const unsigned char Command::header0 = 0xaa;
27 const unsigned char Command::header1 = 0x55;
28 
29 /*****************************************************************************
30 ** Implementation [Command Generators]
31 *****************************************************************************/
32 
53 Command Command::SetLedArray(const enum LedNumber &number, const enum LedColour &colour, Command::Data &current_data)
54 {
55  // gp_out is 16 bits
56  uint16_t value;
57  if (number == Led1)
58  {
59  value = colour; // defined with the correct bit specification.
60  current_data.gp_out = (current_data.gp_out & 0xfcff) | value; // update first
61  }
62  else
63  {
64  value = colour << 2;
65  current_data.gp_out = (current_data.gp_out & 0xf3ff) | value; // update first
66  }
67  Command outgoing;
68  outgoing.data = current_data;
69  outgoing.data.command = Command::SetDigitalOut;
70  return outgoing;
71 }
72 
84 Command Command::SetDigitalOutput(const DigitalOutput &digital_output, Command::Data &current_data)
85 {
86  uint16_t values = 0x0000;
87  uint16_t clear_mask = 0xfff0;
88  for ( unsigned int i = 0; i < 4; ++i ) {
89  if ( digital_output.mask[i] ) {
90  if ( digital_output.values[i] ) {
91  values |= ( 1 << i );
92  }
93  } else {
94  clear_mask |= ( 1 << i ); // don't clear this bit, so set a 1 here
95  }
96  }
97  current_data.gp_out = (current_data.gp_out & clear_mask) | values;
98  Command outgoing;
99  outgoing.data = current_data;
101  return outgoing;
102 }
103 
115 Command Command::SetExternalPower(const DigitalOutput &digital_output, Command::Data &current_data)
116 {
117  uint16_t values = 0x0000;
118  uint16_t clear_mask = 0xff0f;
119  for ( unsigned int i = 0; i < 4; ++i ) {
120  if ( digital_output.mask[i] ) {
121  if ( digital_output.values[i] ) {
122  values |= ( 1 << (i+4) );
123  }
124  } else {
125  clear_mask |= ( 1 << (i+4) ); // don't clear this bit, so set a 1 here
126  }
127  }
128  current_data.gp_out = (current_data.gp_out & clear_mask) | values;
129  Command outgoing;
130  outgoing.data = current_data;
132  return outgoing;
133 }
134 
135 Command Command::PlaySoundSequence(const enum SoundSequences &number, Command::Data & /* current_data */)
136 {
137  uint16_t value; // gp_out is 16 bits
138  value = number; // defined with the correct bit specification.
139 
140  Command outgoing;
141  outgoing.data.segment_name = value;
143  return outgoing;
144 }
145 
147 {
148  Command outgoing;
149  outgoing.data.request_flags = 0;
150  outgoing.data.request_flags |= static_cast<uint16_t>(HardwareVersion);
151  outgoing.data.request_flags |= static_cast<uint16_t>(FirmwareVersion);
152  outgoing.data.request_flags |= static_cast<uint16_t>(UniqueDeviceID);
154  return outgoing;
155 }
156 
158 {
159  Command outgoing;
160  std::vector<short> velocity_commands = diff_drive.velocityCommands();
161  outgoing.data.speed = velocity_commands[0];
162  outgoing.data.radius = velocity_commands[1];
163  outgoing.data.command = Command::BaseControl;
164  return outgoing;
165 }
166 
167 Command Command::SetVelocityControl(const int16_t &speed, const int16_t &radius)
168 {
169  Command outgoing;
170  outgoing.data.speed = speed;
171  outgoing.data.radius = radius;
172  outgoing.data.command = Command::BaseControl;
173  return outgoing;
174 }
175 
176 Command Command::SetControllerGain(const unsigned char &type, const unsigned int &p_gain,
177  const unsigned int &i_gain, const unsigned int &d_gain)
178 {
179  Command outgoing;
180  outgoing.data.type = type;
181  outgoing.data.p_gain = p_gain;
182  outgoing.data.i_gain = i_gain;
183  outgoing.data.d_gain = d_gain;
185  return outgoing;
186 }
187 
189 {
190  Command outgoing;
192  outgoing.data.reserved = 0;
193  return outgoing;
194 }
195 
196 /*****************************************************************************
197 ** Implementation [Serialisation]
198 *****************************************************************************/
202 void Command::resetBuffer(Buffer& buffer) {
203  buffer.clear();
204  buffer.resize(64);
205  buffer.push_back(Command::header0);
206  buffer.push_back(Command::header1);
207  buffer.push_back(0); // just initialise, we usually write in the payload here later (size of payload only, not stx, not etx, not length)
208 }
209 
211 {
212  // need to be sure we don't pass through an emum to the Trans'd buildBytes functions.
213  unsigned char cmd = static_cast<unsigned char>(data.command);
214  unsigned char length = 0;
215  switch (data.command)
216  {
217  case BaseControl:
218  buildBytes(cmd, byteStream);
219  buildBytes(length=4, byteStream);
220  buildBytes(data.speed, byteStream);
221  buildBytes(data.radius, byteStream);
222  break;
223  case Sound:
224  buildBytes(cmd, byteStream);
225  buildBytes(length=3, byteStream);
226  buildBytes(data.note, byteStream);
227  buildBytes(data.duration, byteStream);
228  break;
229  case SoundSequence:
230  buildBytes(cmd, byteStream);
231  buildBytes(length=1, byteStream);
232  buildBytes(data.segment_name, byteStream);
233  break;
234  case RequestExtra:
235  buildBytes(cmd, byteStream);
236  buildBytes(length=2, byteStream);
237  buildBytes(data.request_flags, byteStream);
238  break;
239  case ChangeFrame:
240  buildBytes(cmd, byteStream);
241  buildBytes(length=1, byteStream);
242  buildBytes(data.frame_id, byteStream);
243  break;
244  case RequestEeprom:
245  buildBytes(cmd, byteStream);
246  buildBytes(length=1, byteStream);
247  buildBytes(data.frame_id, byteStream);
248  break;
249  case SetDigitalOut:
250  { // this one controls led, external power sources, gp digitial output
251  buildBytes(cmd, byteStream);
252  buildBytes(length=2, byteStream);
253  buildBytes(data.gp_out, byteStream);
254  break;
255  }
256  case SetController:
257  buildBytes(cmd, byteStream);
258  buildBytes(length=13, byteStream);
259  buildBytes(data.type, byteStream);
260  buildBytes(data.p_gain, byteStream);
261  buildBytes(data.i_gain, byteStream);
262  buildBytes(data.d_gain, byteStream);
263  break;
264  case GetController:
265  buildBytes(cmd, byteStream);
266  buildBytes(length=1, byteStream);
267  buildBytes(data.reserved, byteStream);
268  break;
269  default:
270  return false;
271  break;
272  }
273  return true;
274 }
275 
276 
277 
278 } // namespace kobuki
kobuki::Command::Data::reserved
unsigned char reserved
Definition: command.hpp:120
kobuki::Command::Data::segment_name
unsigned char segment_name
Definition: command.hpp:99
kobuki::LedNumber
LedNumber
Definition: led_array.hpp:39
packet_handler::payloadBase::length
const unsigned char length
Definition: payload_base.hpp:73
kobuki
Definition: command.hpp:31
kobuki::Command::SetDigitalOutput
static Command SetDigitalOutput(const DigitalOutput &digital_output, Command::Data &current_data)
Definition: command.cpp:88
kobuki::Command::data
Data data
Definition: command.hpp:138
kobuki::LedColour
LedColour
Definition: led_array.hpp:44
kobuki::Command::Data::note
uint16_t note
Definition: command.hpp:93
kobuki::Command::RequestEeprom
@ RequestEeprom
Definition: command.hpp:57
kobuki::DiffDrive::velocityCommands
void velocityCommands(const double &vx, const double &wz)
Definition: diff_drive.cpp:140
kobuki::Command::GetVersionInfo
static Command GetVersionInfo()
Definition: command.cpp:150
kobuki::Command::GetController
@ GetController
Definition: command.hpp:58
kobuki::Command::HardwareVersion
@ HardwareVersion
Definition: command.hpp:63
kobuki::Command::UniqueDeviceID
@ UniqueDeviceID
Definition: command.hpp:63
kobuki::Command::Data::command
Name command
Definition: command.hpp:86
kobuki::Command::Data::speed
int16_t speed
Definition: command.hpp:89
kobuki::Command::SetLedArray
static Command SetLedArray(const enum LedNumber &number, const enum LedColour &colour, Command::Data &current_data)
Definition: command.cpp:57
kobuki::Command::Data::radius
int16_t radius
Definition: command.hpp:90
kobuki::Command::Data::frame_id
unsigned char frame_id
Definition: command.hpp:105
kobuki::Command::Data::i_gain
unsigned int i_gain
Definition: command.hpp:116
kobuki::Command::SoundSequence
@ SoundSequence
Definition: command.hpp:57
kobuki::Command::SetDigitalOut
@ SetDigitalOut
Definition: command.hpp:58
kobuki::Command::SetController
@ SetController
Definition: command.hpp:58
radius
double radius
Definition: velocity_commands.cpp:18
kobuki::Command::SetExternalPower
static Command SetExternalPower(const DigitalOutput &digital_output, Command::Data &current_data)
Definition: command.cpp:119
kobuki::Command::PlaySoundSequence
static Command PlaySoundSequence(const enum SoundSequences &number, Command::Data &current_data)
Definition: command.cpp:139
kobuki::Command::Data::request_flags
uint16_t request_flags
Definition: command.hpp:102
speed
double speed
Definition: velocity_commands.cpp:18
kobuki::Command::serialise
bool serialise(ecl::PushAndPop< unsigned char > &byteStream)
Definition: command.cpp:214
kobuki::Command::RequestExtra
@ RequestExtra
Definition: command.hpp:57
packet_handler::payloadBase::buildBytes
void buildBytes(const T &V, ecl::PushAndPop< unsigned char > &buffer)
Definition: payload_base.hpp:110
kobuki::Command::SetVelocityControl
static Command SetVelocityControl(DiffDrive &diff_drive)
Definition: command.cpp:161
kobuki::Command::resetBuffer
void resetBuffer(Buffer &buffer)
Definition: command.cpp:206
ecl::PushAndPop< unsigned char >
kobuki::Command
Definition: command.hpp:40
kobuki::Command::Sound
@ Sound
Definition: command.hpp:57
kobuki::DiffDrive
Definition: diff_drive.hpp:44
kobuki::Command::Data::gp_out
uint16_t gp_out
Definition: command.hpp:111
kobuki::Command::SetControllerGain
static Command SetControllerGain(const unsigned char &type, const unsigned int &p_gain, const unsigned int &i_gain, const unsigned int &d_gain)
Definition: command.cpp:180
kobuki::Command::FirmwareVersion
@ FirmwareVersion
Definition: command.hpp:63
kobuki::Led1
@ Led1
Led1.
Definition: led_array.hpp:48
kobuki::Command::Data::duration
unsigned char duration
Definition: command.hpp:94
kobuki::Command::header0
static const unsigned char header0
Definition: command.hpp:145
kobuki::Command::BaseControl
@ BaseControl
Definition: command.hpp:57
kobuki::Command::ChangeFrame
@ ChangeFrame
Definition: command.hpp:57
kobuki::Command::Data::p_gain
unsigned int p_gain
Definition: command.hpp:115
kobuki::Command::Data::type
unsigned char type
Definition: command.hpp:114
kobuki::Command::GetControllerGain
static Command GetControllerGain()
Definition: command.cpp:192
kobuki::Command::header1
static const unsigned char header1
Definition: command.hpp:146
kobuki::SoundSequences
SoundSequences
Definition: sound.hpp:36
kobuki::Command::Data::d_gain
unsigned int d_gain
Definition: command.hpp:117


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Wed Mar 2 2022 00:26:14