cmd_server.cpp
Go to the documentation of this file.
1 
10 
11 using namespace Trajectory;
12 
17  if (ready_move_) {
18  robot_->enableRobot();
19  robot_->readySound();
20  int arm_pos = 20;
21  int lean_pos = 30;
22  this->lean(CMD_LEFT, lean_pos, 500);
23  this->lean(CMD_RIGHT, lean_pos, 1000);
24  this->arms(0, 0);
25  this->standStraight(500);
26 
27 
29  this->arms(arm_pos, arm_pos);
30  robot_->setServo(EYES, EYES_NORMAL);
31  sleepms(stop_wait * 2);
32  this->arms(0, 0);
33  float batt = robot_->getBattery();
34  robot_->setServo(EYES, EYES_WIDE * (1.0 - batt));
36  robot_->stopRobot();
37  }
38 }
39 
48 void CmdServer::runCommand(vector<int> data) {
49  busy_ = true;
50  int l = data.size();
51  switch (data[0]) {
52  case CMD_HELLO: hello(); break;
53  case CMD_MOVEJOINT:
54  if (l == 4) {moveJoint(data[1], data[2], data[3]);}
55  if (l == 5) {moveJoint(data[1], data[2], data[3], data[4]);} break;
56  case CMD_LEAN:
57  if (l == 2) {lean(data[1]);} if (l == 3) {lean(data[1], data[2]);}
58  if (l == 4) {lean(data[1], data[2], data[3]);} break;
59  case CMD_WALK:
60  if (l == 2) {walk(data[1]);} if (l == 3) {walk(data[1], data[2]);}
61  if (l == 4) {walk(data[1], data[2], data[3]);}
62  if (l == 5) {walk(data[1], data[2], data[3], data[4]);}
63  if (l == 6) {walk(data[1], data[2], data[3], data[4], data[5]);} break;
64  case CMD_KICK:
65  if (l == 2) {kick(data[1]);} if (l == 3) {kick(data[1], data[2]);} break;
66  case CMD_EYES:
67  if (l == 1) {eyes();} if (l == 2) {eyes(data[1]);}
68  if (l == 3) {eyes(data[1], data[2]);} break;
69  case CMD_CELEBRATE:
70  if (l == 2) {celebrate(data[1]);} break;
71  case CMD_LIFTLEG:
72  if (l == 2) {liftLeg(data[1]);} if (l == 3) {liftLeg(data[1], data[2]);}
73  if (l == 4) {liftLeg(data[1], data[2], data[3]);} break;
74  case CMD_LOWERLEG:
75  if (l == 2) {lowerLeg(data[1]);} break;
76  case CMD_DANCE:
77  dance(data[1]); break;
78  case CMD_ROLLERSKATE: rollerSkate(robot_); break;
79  case CMD_ARMS:
80  if (l == 2) {arms(data[1]);} if (l == 3) {arms(data[1], data[2]);} break;
81  case CMD_DEMO: demo(); break;
82  case CMD_GET: if (l == 3) {getData(data[1], data[2]);} break;
83  case CMD_SIDESTEP:
84  if (l == 2) {sideStep(data[1]);} if (l == 3) {sideStep(data[1], data[2]);}
85  if (l == 4) {sideStep(data[1], data[2], data[3]);}
86  if (l == 5) {sideStep(data[1], data[2], data[3], data[4]);} break;
87  case CMD_STRAIGHT:
88  if (l == 1) {standStraight();} if (l == 2) {standStraight(data[1]);} break;
89  case CMD_SOUND:
90  playSounds(data); break;
91  case CMD_STOP: stopRobot(); break;
92  default:
93  ROS_ERROR_STREAM("CmdServer did not recognise command " << data[0]); break;
94  }
95  busy_ = false;
96 }
97 
98 void CmdServer::runSockCommand(vector<int8_t> data) {
99  if ((data.size() % 4) != 0) {ROS_ERROR("Data Package wrong size!");} else {
100  vector<int> cmd_data;
101  for (int i = 0; i < (data.size() / 4); ++i) {
102  // ROS_INFO_STREAM("Data " << i << ": " << (int)data[i]);
103  cmd_data.push_back(0);
104  memcpy(&cmd_data[i], &data[(i * 4)], 4);
105  // ROS_INFO_STREAM("NewData " << cmd_data[i]);
106  }
107  // if (busy_) {ROS_INFO("BUSY!");} else {ROS_INFO("NOT BUSY!");}
108  if (!busy_) { runCommand(cmd_data); }
109  else { cmd_queue_.push_back(cmd_data); }
110  }
111 }
112 
114  data_t tInterp;
115  // Return body to center
116  tInterp = genReturnToZero(robot_, 1.5);
117  runTrajectory(robot_, tInterp);
118  // Move eyes
120  robot_->setServo(EYES, EYES_WIDE);
121  sleepms(stop_wait * 3);
122  robot_->setServo(EYES, EYES_NORMAL);
124 }
125 
126 void CmdServer::walk(int num_steps, int turn, int move_time, int step_length,
127  int side) {
128  data_t tInterp;
129  float step_time = ((float) move_time) / 1000; // Float in seconds
130  bool leftFoot = false;
131  if (side == CMD_LEFT) { leftFoot = true; }
132  else if (side == CMD_RIGHT) { leftFoot = false; } else {
133  // Step with left foot if LHip is ahead
134  if (robot_->jangles_[RHIP] < robot_->jangles_[LHIP]) { leftFoot = true; }
135  // Invert if walking backwards
136  if (step_length < 0) { leftFoot = !leftFoot; }
137  }
138  if ((turn_spot_) && ((abs(turn) > 0) && (step_length == 0)))
139  { leftFoot = !leftFoot; turn_spot_ = false; }
140  else if ((abs(turn) > 0) && (step_length == 0)) { turn_spot_ = true; }
141  else { turn_spot_ = false; }
142  // Start walking!
143  for (int step_num = 0; step_num < num_steps; step_num++) {
144  if (leftFoot) {
145  tInterp = genStepLeft(robot_, step_length, turn, step_time);
146  } else {
147  tInterp = genStepRight(robot_, step_length, turn, step_time);
148  }
149  runTrajectory(robot_, tInterp);
150  leftFoot = !leftFoot;
151  }
152 }
153 
154 void CmdServer::kick(int side, int move_time) {
155  data_t tInterp;
156  float kicktime = ((float)move_time) / 1000;
157  if (side == CMD_LEFT) {
158  // robot_->loadKeyframes("kick_left", move_time);
159  tInterp = genKickLeft(robot_, kicktime);
160  } else {
161  // robot_->loadKeyframes("kick_right", move_time);
162  tInterp = genKickRight(robot_, kicktime);
163  }
164  runTrajectory(robot_, tInterp);
165 }
166 
167 void CmdServer::eyes(int amount, int amount2) {
168  robot_->setServo(EYES, amount);
169 }
170 
171 void CmdServer::getData(int sensor, int id) {
172  resp_request_ = true;
173  if (sensor == GET_GPIO) {
174  if ((id >= 8) || (id < 0)) {resp_request_ = false;}
175  else { val_request_ = gpio_data_.gpio[id]; }
176  } else if (sensor == GET_ACCEL) {
177  if (id == AXES_X) { val_request_ = accel_data_.x;}
178  else if (id == AXES_Y) { val_request_ = accel_data_.y;}
179  else if (id == AXES_Z) { val_request_ = accel_data_.z;}
180  else {resp_request_ = false;}
181  } else if (sensor == GET_BATT) {
182  val_request_ = batt_data_.data;
183  } else if (sensor == GET_CURR) {
184  if ((id >= 8) || (id < 0)) {resp_request_ = false;}
185  else { val_request_ = curr_data_.current[id]; }
186  } else if (sensor == GET_BALL) {
187  if (id == AXES_X) { val_request_ = ball_pos_.x;}
188  else if (id == AXES_Y) { val_request_ = ball_pos_.y;}
189  } else {
190  resp_request_ = false;
191  }
192 }
193 
194 void CmdServer::moveJoint(int side, int joint, int amount, int move_time) {
195  data_t tSetpoints, tInterp;
196  deque<float> tline(robot_->jangles_); tline.push_front(0);
197  // Generate a trajectory to move the joint
198  // first line is the robot's present position
199  tSetpoints.clear(); tSetpoints.push_back(tline);
200  if (joint == J_HIP) {
201  if (side == CMD_LEFT) {
202  tline[1 + LHIP] = (float)amount;
203  } else if (side == CMD_RIGHT) {
204  tline[1 + RHIP] = (float)amount;
205  }
206  } else if (joint == J_TWIST) {
207  if (side == CMD_LEFT) {
208  tline[1 + LTWIST] = (float)amount;
209  } else if (side == CMD_RIGHT) {
210  tline[1 + RTWIST] = (float)amount;
211  }
212  } else if (joint == J_KNEE) {
213  if (side == CMD_LEFT) {
214  tline[1 + LKNEE] = (float)amount;
215  } else if (side == CMD_RIGHT) {
216  tline[1 + RKNEE] = (float)amount;
217  }
218  } else if (joint == J_LEG) {
219  if (side == CMD_LEFT) {
220  tline[1 + LHIP] = (float)amount;
221  } else if (side == CMD_RIGHT) {
222  tline[1 + RHIP] = (float)amount;
223  }
224  } else if (joint == J_ARM) {
225  if (side == CMD_LEFT) {
226  tline[1 + LARM] = (float)amount;
227  } else if (side == CMD_RIGHT) {
228  tline[1 + RARM] = (float)amount;
229  }
230  } else if (joint == J_EYES) {
231  tline[1 + EYES] = (float)amount;
232  }
233  tline[0] = ((float)move_time) / 1000; tSetpoints.push_back(tline);
234  interpTrajectory(tSetpoints, tInterp, interp_dt_);
235  runTrajectory(robot_, tInterp);
236 }
237 
238 void CmdServer::lean(int dir, int amount, int move_time) {
239  data_t tSetpoints, tInterp; deque<float> tline(robot_->jangles_);
240  tline.push_front(0); tSetpoints.push_back(tline);
241  float lean_time = ((float)move_time) / 1000;
242  if (dir == CMD_LEFT) {setPointsLeanLeft(tSetpoints, amount, lean_time);}
243  if (dir == CMD_RIGHT) {setPointsLeanRight(tSetpoints, amount, lean_time);}
244  if (dir == CMD_FORW) {setPointsLeanForward(tSetpoints, amount, lean_time);}
245  if (dir == CMD_BACK) {setPointsLeanBackward(tSetpoints, amount, lean_time);}
246  interpTrajectory(tSetpoints, tInterp, interp_dt_);
247  runTrajectory(robot_, tInterp);
248 }
249 
250 void CmdServer::celebrate(int move_time) {
251  data_t tInterp;
252  tInterp = genCelebration(robot_, ((float)move_time) / 1000);
253  runTrajectory(robot_, tInterp);
254  robot_->celebSound(1.6);
255 }
256 
257 // Generate a trajectory to move the knee
258 // First line is the robot's present position
259 // tline should already have one line,
260 // with 0.0 timecode and robot's present angles
261 void CmdServer::liftLeg(int leg, int amount, int move_time) {
262  data_t tSetpoints, tInterp; deque<float> tline(robot_->jangles_);
263  tline.push_front(0); tSetpoints.clear(); tSetpoints.push_back(tline);
264  if (leg == CMD_LEFT) {
265  tline[1 + LKNEE] = tline[1 + RKNEE] + (float)amount;
266  } else if (leg == CMD_RIGHT) {
267  tline[1 + RKNEE] = tline[1 + LKNEE] - (float)amount;
268  }
269  tline[0] = ((float)move_time) / 1000; tSetpoints.push_back(tline);
270  interpTrajectory(tSetpoints, tInterp, interp_dt_);
271  runTrajectory(robot_, tInterp);
272 }
273 
274 // This will lower whichever leg is higher to the ground
275 // Initial implementation just thinks about the knees.
276 // TODO: should consider hip angle too,
277 // then adjust knee angle only to get foot to floor
278 // This function will never change the sign of the knee angle
279 void CmdServer::lowerLeg(int move_time) {
280  data_t tSetpoints, tInterp; deque<float> tline(robot_->jangles_);
281  tline.push_front(0); tSetpoints.push_back(tline);
282 
283  if (abs(robot_->jangles_[RKNEE]) > abs(robot_->jangles_[LKNEE])) {
284  // Right knee is higher than left knee
285  if (robot_->jangles_[RKNEE] < 0) {
286  tline[1 + RKNEE] = 0 - abs(robot_->jangles_[LKNEE]);
287  } else {
288  tline[1 + RKNEE] = abs(robot_->jangles_[LKNEE]);
289  }
290  } else { // Left knee is higher than right knee
291  if (robot_->jangles_[LKNEE] < 0) {
292  tline[1 + LKNEE] = 0 - abs(robot_->jangles_[RKNEE]);
293  } else {
294  tline[1 + LKNEE] = abs(robot_->jangles_[RKNEE]);
295  }
296  }
297  tline[0] = ((float)move_time) / 1000; tSetpoints.push_back(tline);
298  interpTrajectory(tSetpoints, tInterp, interp_dt_);
299  runTrajectory(robot_, tInterp);
300 }
301 
302 void CmdServer::dance(int robot_id) {
303  if (robot_id < 0) { robot_id = 0; } if (robot_id > 2) { robot_id = 2; }
304  hipToBeSquare(robot_, robot_id);
305 }
306 
307 void CmdServer::arms(int r_angle, int l_angle) {
308  std::map<int, float> angles = {{RARM, (float)r_angle}, {LARM, (float)l_angle}};
309  robot_->setServos(angles);
310 }
311 
313  float alpha = 0;
314  int delay = (int)(INTERP_DT * 1000.0);
315  std::map<int, float> angles;
316  for (int i = 0; i < NUMJOINTS; ++i) {
317  angles[i] = robot_->jangles_[i];
318  }
319  this->eyes(EYES_NORMAL);
320  sleep(1);
321  this->eyes(EYES_ANGRY);
322  sleep(1);
323  this->eyes(EYES_EXCITED);
324  sleep(1);
325  this->eyes(EYES_WIDE);
326  sleep(1);
327 
328  // Test eyes
329  for (alpha = 0; alpha < 6.28; alpha += 0.1) {
330  robot_->setServo(EYES, EYES_ANGRY * sin(alpha));
331  sleepms(delay);
332  }
333 
334  this->swingArms(100, 100, 2000, 8); // Arms Swing Together
335  this->swingArms(100, -100, 2000, 8); // Arms Swing Opposed
336 
337  marty_msgs::ServoMsgArray h_array;
338  marty_msgs::ServoMsg r_hip; r_hip.servo_id = RHIP; r_hip.servo_cmd = 45;
339  marty_msgs::ServoMsg l_hip; l_hip.servo_id = LHIP; l_hip.servo_cmd = 45;
340  h_array.servo_msg.push_back(r_hip); h_array.servo_msg.push_back(l_hip);
341  this->swingJoints(h_array, 2000, 8);
342 
343  // Forward and Backward
344  for (alpha = 0; alpha < 12.56; alpha += 0.1) {
345  angles[RHIP] = 45 * sin(alpha); angles[LHIP] = angles[RHIP];
346  robot_->setServos(angles);
347  sleepms(delay);
348  }
349 
350  // Side to Side
351  for (alpha = 0; alpha < 12.56; alpha += 0.1) {
352  angles[RKNEE] = 45 * sin(alpha); angles[LKNEE] = angles[RKNEE];
353  robot_->setServos(angles);
354  sleepms(delay);
355  }
356 
357  // Feet Twist in same direction
358  angles[RHIP] = 0; angles[RKNEE] = 0; angles[LHIP] = 0;
359  angles[LKNEE] = 0;
360  for (alpha = 0; alpha < 12.56; alpha += 0.1) {
361  angles[RTWIST] = 45 * sin(alpha); angles[LTWIST] = angles[RTWIST];
362  robot_->setServos(angles);
363 
364  sleepms(delay);
365  }
366 
367  // Feet Twist in opposite direction
368  for (alpha = 0; alpha < 12.56; alpha += 0.1) {
369  angles[RTWIST] = 20 * sin(alpha); angles[LTWIST] = 0 - angles[RTWIST];
370  robot_->setServos(angles);
371  sleepms(delay);
372  }
373 
374  this->testLeg(CMD_LEFT, 4.0); // Test Left Leg
375  this->testLeg(CMD_RIGHT, 4.0); // Test Right Leg
376 
377  data_t genTraj;
378  // Walk about
379  for (int numstep = 0; numstep < 1; numstep++) {
380  genTraj = genStepLeft(robot_, 0, 25, 1.25);
381  runTrajectory(robot_, genTraj);
382  genTraj.clear();
383 
384  genTraj = genStepRight(robot_, 0, 25, 1.25);
385  runTrajectory(robot_, genTraj);
386  genTraj.clear();
387  }
388 
389  for (int numstep = 0; numstep < 2; numstep++) {
390  genTraj = genStepLeft(robot_, 80, 0, 1.5);
391  runTrajectory(robot_, genTraj);
392  genTraj.clear();
393 
394  genTraj = genStepRight(robot_, 80, 0, 1.5);
395  runTrajectory(robot_, genTraj);
396  genTraj.clear();
397  }
398 
399  // Test a Left Kick
400  genTraj = genKickLeft(robot_, 2.0);
401  runTrajectory(robot_, genTraj);
402  genTraj.clear();
403 
404  // Celebrate!
405  genTraj = genCelebration(robot_, 4.0);
406  runTrajectory(robot_, genTraj);
407  genTraj.clear();
408  robot_->celebSound(1.4);
409 }
410 
411 void CmdServer::testLeg(int side, int duration) {
412  data_t genTraj;
413  if (side == CMD_LEFT) {
414  genTraj = genRaisedFootTwistLeft(robot_, ((float)duration) / 1000);
415  runTrajectory(robot_, genTraj);
416  } else if (side == CMD_RIGHT) {
417  genTraj = genRaisedFootTwistRight(robot_, ((float)duration) / 1000);
418  runTrajectory(robot_, genTraj);
419  }
420 }
421 
422 void CmdServer::sitBack(int duration) {
423  data_t genTraj = genSitBack(robot_, ((float)duration) / 1000);
424  runTrajectory(robot_, genTraj);
425 }
426 
427 void CmdServer::swingArms(int r_arm, int l_arm, int duration, int cycles) {
428  if (duration <= 0) { duration = 2000; }
429  if (cycles <= 0) { cycles = 4; }
430  int delay = (int)(INTERP_DT * 1000.0);
431  int steps = duration / delay;
432 
433  std::map<int, float> angles;
434  for (int i = 0; i < NUMJOINTS; ++i) {
435  angles[i] = robot_->jangles_[i];
436  }
437  float alpha = 0;
438  float inc = (1.57 * cycles) / steps;
439  for (int s = 0; s < steps; ++s) {
440  angles[RARM] = r_arm * sin(alpha);
441  angles[LARM] = l_arm * sin(alpha);
442  robot_->setServos(angles);
443  sleepms(delay);
444  alpha += inc;
445  }
446 }
447 
448 void CmdServer::swingJoints(marty_msgs::ServoMsgArray servo_array, int duration,
449  int cycles) {
450  if (duration <= 0) { duration = 2000; }
451  if (cycles <= 0) { cycles = 4; }
452  int delay = (int)(INTERP_DT * 1000.0);
453  int steps = duration / delay;
454 
455  std::map<int, float> angles;
456  for (int i = 0; i < NUMJOINTS; ++i) {
457  angles[i] = robot_->jangles_[i];
458  }
459  float alpha = 0;
460  float inc = (1.57 * cycles) / steps;
461  for (int s = 0; s < steps; ++s) {
462  for (int i = 0; i < servo_array.servo_msg.size(); ++i) {
463  angles[servo_array.servo_msg[i].servo_id] =
464  servo_array.servo_msg[i].servo_cmd * sin(alpha);
465  }
466  robot_->setServos(angles);
467  sleepms(delay);
468  alpha += inc;
469  }
470 }
471 
472 void CmdServer::sideStep(int side, int num_steps, int move_time,
473  int step_length) {
474  if ((side == CMD_LEFT) || (side == CMD_RIGHT)) {
475  int opp_side = 1 - side; // Switch left/right
476  if (side == CMD_LEFT) {step_length = -step_length;} // Invert step direction
477  for (int step = 0; step < num_steps; ++step) {
478  this->moveJoint(side, J_KNEE, -step_length, (move_time / 4));
479  this->moveJoint(opp_side, J_KNEE, step_length, (move_time / 4));
480  this->moveJoint(side, J_KNEE, 0, (move_time / 4));
481  this->moveJoint(opp_side, J_KNEE, 0, (move_time / 4));
482  }
483  } else { ROS_ERROR("Can only sidestep left or right"); }
484 }
485 
486 void CmdServer::standStraight(int move_time) {
487  data_t tSetpoints, tInterp; deque<float> tline(robot_->jangles_);
488  tline.push_front(0); tSetpoints.push_back(tline);
489  float straight_time = ((float)move_time) / 1000;
490  setPointsLegsZero(tSetpoints, straight_time);
491  interpTrajectory(tSetpoints, tInterp, interp_dt_);
492  runTrajectory(robot_, tInterp);
493 }
494 
495 // void CmdServer::playSound(int frequency, int duration, int freq2) {
496 // robot_->playSound(frequency, (float)duration / 1000, freq2);
497 // }
498 
499 void CmdServer::playSounds(std::vector<int> sounds) {
500  marty_msgs::SoundArray sound_msg;
501  if (((sounds.size() - 1) % 3) != 0) {
502  ROS_WARN("Received wrong number of sound arguments!");
503  } else {
504  for (int s = 0; s < ((sounds.size() - 1) / 3); ++s) {
505  marty_msgs::Sound sound;
506  int i = 1 + (s * 3);
507  sound.freq1 = sounds[i];
508  sound.duration = ((float)sounds[i + 1] / 1000);
509  sound.freq2 = sounds[i + 2];
510  sound_msg.sound.push_back(sound);
511  }
512  robot_->playSoundArray(sound_msg);
513  }
514 }
515 
518  robot_->stopRobot();
519 }
520 
521 // SERVER FUNCTIONS, DO NOT TOUCH!
522 volatile sig_atomic_t exiting = 0;
523 void exit_f(int sig) {
524  ROS_WARN("EXITING!");
525  ros::param::del("/marty");
526  ros::shutdown();
527  exiting = 1;
528 }
529 
530 CmdServer::CmdServer(ros::NodeHandle& nh) : nh_(nh) {
531  this->loadParams();
532  this->init();
533  this->rosSetup();
534  ROS_INFO("CmdServer Ready!");
535 }
536 
538  ros::param::del("/marty");
539 }
540 
542  bool launched;
543  nh_.param("launched", launched, false);
544  if (!launched) {
545  ROS_ERROR("Please use 'roslaunch ros_marty marty.launch'\n");
546  ros::shutdown();
547  }
548  nh_.param("ready_move", ready_move_, true);
549 }
550 
552  robot_ = new MartyCore(nh_);
553  busy_ = false;
554  ros_cmd_ = false;
555  resp_request_ = false;
556  interp_dt_ = 0.02;
557  turn_spot_ = false;
558 }
559 
561  gpio_sub_ = nh_.subscribe("gpios", 1000, &CmdServer::gpioCB, this);
562  accel_sub_ = nh_.subscribe("accel", 1000, &CmdServer::accelCB, this);
563  batt_sub_ = nh_.subscribe("battery", 1000, &CmdServer::battCB, this);
564  curr_sub_ = nh_.subscribe("motor_currents", 1000, &CmdServer::currCB, this);
565  ball_sub_ = nh_.subscribe("ball_pos", 1000, &CmdServer::ballCB, this);
566  cmd_srv_ = nh_.advertiseService("command", &CmdServer::cmd_service, this);
567 }
568 
569 bool CmdServer::cmd_service(marty_msgs::Command::Request& req,
570  marty_msgs::Command::Response& res) {
571  if (!busy_) { ros_cmd_ = true; cmd_data_ = req.data; res.success = true; }
572  else { res.success = false; }
573  return true;
574 }
575 
577  // Create socket, initialise and listen
578  struct sockaddr_in servaddr;
579  sock_ = socket(AF_INET, SOCK_STREAM, 0);
580  if (sock_ < 0) { ROS_ERROR("cannot create socket"); ros::shutdown(); }
581  int yes = 1;
582  setsockopt(sock_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
583 
584  memset((char*)&servaddr, 0, sizeof(servaddr));
585  servaddr.sin_family = AF_INET;
586  servaddr.sin_addr.s_addr = INADDR_ANY;
587  servaddr.sin_port = htons(PORT);
588  if (bind(sock_, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
589  { ROS_ERROR("ERROR on binding"); ros::shutdown(); }
590  listen(sock_, 5);
591 
592  // set server socket to non-binding, so that accept will not hang
593  fcntl(sock_, F_SETFL, O_NONBLOCK);
594  printf("Socket initialised on port: %d. Waiting for incoming connection\n",
595  PORT);
596 }
597 
599  close(sock_);
600 }
601 
603  struct sockaddr_in clientaddr;
604  char buffer[4096];
605  char const* resp_msg = "";
606  socklen_t clilen = sizeof(clientaddr);
607  int clisock = accept(sock_, (struct sockaddr*) &clientaddr, &clilen);
608  if (clisock >= 0) {
609  robot_->enableRobot();
610  ROS_DEBUG("Incoming connection accepted...\n");
611  int nbytes = read(clisock, buffer, 4096);
612  if (nbytes > 0) {
613  ROS_DEBUG("Data received: %d", buffer[0]);
614  vector<int8_t> dbytes;
615  dbytes.push_back(buffer[0]);
616  for (int i = 1; i < nbytes; i++) {
617  ROS_DEBUG("\t%d", buffer[i]);
618  dbytes.push_back(buffer[i]);
619  }
620  ROS_DEBUG("Running Command\n");
621  bool get = false;
622  if (dbytes[0] == CMD_GET) { get = true; }
623  if (robot_->hasFallen() && robot_->fallDisabled() && !get) {
624  ROS_WARN("Marty has fallen over! Please pick him up and try again.");
625  resp_msg = "Fallen";
626  } else {
627  runSockCommand(dbytes);
628  resp_msg = "Success";
629  }
630  ROS_DEBUG("Done!\n");
631  }
632  if (resp_request_) {
633  write(clisock, (char*)&val_request_, sizeof(val_request_));
634  resp_request_ = false;
635  } else {
636  write(clisock, resp_msg, strlen(resp_msg));
637  }
638  close(clisock);
639  } else if (ros_cmd_) {
640  if (robot_->hasFallen() && robot_->fallDisabled()) {
641  ROS_WARN("Marty has fallen over! Please pick him up and try again.");
642  robot_->stopRobot();
643  } else {
644  robot_->enableRobot();
645  if (!busy_) { runCommand(cmd_data_); }
646  else { cmd_queue_.push_back(cmd_data_); }
647  }
648  ros_cmd_ = false;
649  } else if ((!busy_) && (cmd_queue_.size() > 0)) {
650  if (robot_->hasFallen() && robot_->fallDisabled()) {
651  ROS_WARN("Marty has fallen over! Please pick him up and try again.");
652  robot_->stopRobot();
653  } else {
654  ROS_INFO_STREAM("RUNNING NEXT CMD: " << cmd_queue_[0][0]);
656  }
657  cmd_queue_.erase(cmd_queue_.begin());
658  }
659 }
660 
661 int main(int argc, char** argv) {
662  signal(SIGINT, exit_f);
663  ros::init(argc, argv, "cmd_server");
664  ros::NodeHandle nh;
665 
666  CmdServer cmd_server(nh);
667  cmd_server.robotReady();
668  cmd_server.setupServer();
669 
670  ros::Rate r(50);
671  while (ros::ok() && !exiting) {
672  cmd_server.waitForCmd();
673  ros::spinOnce();
674  r.sleep();
675  }
676  cmd_server.stopServer();
677  ros::shutdown();
678  return 0;
679 }
void sideStep(int side, int num_steps=1, int move_time=1000, int step_length=50)
Definition: cmd_server.cpp:472
void arms(int r_angle=0, int l_angle=0)
Definition: cmd_server.cpp:307
#define EYES_ANGRY
void sitBack(int duration)
Definition: cmd_server.cpp:422
void stopRobot()
Definition: cmd_server.cpp:516
void demo()
Definition: cmd_server.cpp:312
bool setPointsLeanBackward(data_t &tSetpoints, int leanAmount, float period)
Definition: trajectory.cpp:515
ros::Subscriber curr_sub_
Definition: cmd_server.hpp:164
void moveJoint(int side, int joint, int amount, int move_time=2000)
Definition: cmd_server.cpp:194
data_t genStepLeft(MartyCore *robot, int stepLength, int turn, float period)
Definition: trajectory.cpp:83
void liftLeg(int leg, int amount=100, int move_time=2000)
Definition: cmd_server.cpp:261
void walk(int num_steps=2, int turn=0, int move_time=3000, int step_length=50, int side=-1)
Definition: cmd_server.cpp:126
void dance(int robot_id)
Definition: cmd_server.cpp:302
std::vector< int > cmd_data_
Definition: cmd_server.hpp:151
void waitForCmd()
Definition: cmd_server.cpp:602
bool setPointsLeanForward(data_t &tSetpoints, int leanAmount, float period)
Definition: trajectory.cpp:504
void swingArms(int r_arm, int l_arm, int duration=2000, int cycles=4)
Definition: cmd_server.cpp:427
float interp_dt_
Definition: cmd_server.hpp:150
ros::ServiceServer cmd_srv_
Definition: cmd_server.hpp:166
void loadParams()
Definition: cmd_server.cpp:541
volatile sig_atomic_t exiting
Definition: cmd_server.cpp:522
void runCommand(vector< int > data)
Runs commands received by the server.
Definition: cmd_server.cpp:48
void accelCB(const marty_msgs::Accelerometer &msg)
Definition: cmd_server.hpp:131
#define EYES_EXCITED
bool turn_spot_
Definition: cmd_server.hpp:142
void ballCB(const geometry_msgs::Pose2D &msg)
Definition: cmd_server.hpp:134
bool setPointsLeanLeft(data_t &tSetpoints, int leanAmount, float period)
Definition: trajectory.cpp:478
CmdServer(ros::NodeHandle &nh)
Definition: cmd_server.cpp:530
bool ready_move_
Definition: cmd_server.hpp:145
ros::Subscriber batt_sub_
Definition: cmd_server.hpp:163
bool hasFallen()
Definition: marty_core.hpp:88
void celebrate(int move_time=4000)
Definition: cmd_server.cpp:250
void rosSetup()
Definition: cmd_server.cpp:560
ros::Subscriber gpio_sub_
Definition: cmd_server.hpp:161
void eyes(int amount=0, int amount2=0)
Definition: cmd_server.cpp:167
ros::Subscriber ball_sub_
Definition: cmd_server.hpp:165
MartyCore * robot_
Definition: cmd_server.hpp:148
void hello()
Definition: cmd_server.cpp:113
void init()
Definition: cmd_server.cpp:551
data_t genStepRight(MartyCore *robot, int stepLength, int turn, float period)
Definition: trajectory.cpp:141
void stopRobot()
Definition: marty_core.cpp:589
data_t genKickLeft(MartyCore *robot, float period)
Definition: trajectory.cpp:318
void testLeg(int side, int duration)
Definition: cmd_server.cpp:411
CMD Server for Marty, enabling execution of CMDs remotely.
#define stop_wait
Definition: cmd_server.hpp:35
bool runTrajectory(MartyCore *robot, data_t &traj)
Definition: trajectory.cpp:63
bool setPointsLegsZero(data_t &tSetpoints, float period)
Definition: trajectory.cpp:526
int main(int argc, char **argv)
Definition: cmd_server.cpp:661
void enableRobot()
Definition: marty_core.cpp:584
void runSockCommand(vector< int8_t > data)
Definition: cmd_server.cpp:98
data_t genSitBack(MartyCore *robot, float period)
Definition: trajectory.cpp:125
bool cmd_service(marty_msgs::Command::Request &req, marty_msgs::Command::Response &res)
Definition: cmd_server.cpp:569
data_t genCelebration(MartyCore *robot, float move_time)
Definition: trajectory.cpp:184
bool ros_cmd_
Definition: cmd_server.hpp:140
void sleepms(int ms)
Definition: definitions.hpp:27
bool fallDisabled()
Definition: marty_core.hpp:89
bool interpTrajectory(data_t tIn, data_t &tOut, float dt)
Definition: trajectory.cpp:16
void setupServer()
Definition: cmd_server.cpp:576
void stopServer()
Definition: cmd_server.cpp:598
void getData(int sensor, int id)
Definition: cmd_server.cpp:171
bool setPointsLeanRight(data_t &tSetpoints, int leanAmount, float period)
Definition: trajectory.cpp:491
int hipToBeSquare(MartyCore *robot, int robotID)
Definition: trajectory.cpp:927
std::vector< std::vector< int > > cmd_queue_
Definition: cmd_server.hpp:152
void kick(int side=CMD_LEFT, int move_time=3000)
Definition: cmd_server.cpp:154
data_t genKickRight(MartyCore *robot, float period)
Definition: trajectory.cpp:359
void swingJoints(marty_msgs::ServoMsgArray, int duration=2000, int cycles=4)
Definition: cmd_server.cpp:448
int rollerSkate(MartyCore *robot)
ros::NodeHandle nh_
Definition: cmd_server.hpp:93
void currCB(const marty_msgs::MotorCurrents &msg)
Definition: cmd_server.hpp:133
#define EYES_NORMAL
data_t genRaisedFootTwistRight(MartyCore *robot, float period)
Definition: trajectory.cpp:271
void exit_f(int sig)
Definition: cmd_server.cpp:523
data_t genRaisedFootTwistLeft(MartyCore *robot, float period)
Definition: trajectory.cpp:224
void playSounds(std::vector< int > sounds)
Definition: cmd_server.cpp:499
bool resp_request_
Definition: cmd_server.hpp:141
void standStraight(int move_time=1000)
Definition: cmd_server.cpp:486
void battCB(const std_msgs::Float32 &msg)
Definition: cmd_server.hpp:132
float val_request_
Definition: cmd_server.hpp:153
void gpioCB(const marty_msgs::GPIOs &msg)
Definition: cmd_server.hpp:130
void lean(int dir, int amount=100, int move_time=2000)
Definition: cmd_server.cpp:238
#define INTERP_DT
Definition: trajectory.hpp:18
void robotReady()
Robot motion executed when cmdServer is ready.
Definition: cmd_server.cpp:16
ros::Subscriber accel_sub_
Definition: cmd_server.hpp:162
void lowerLeg(int move_time=1000)
Definition: cmd_server.cpp:279
#define EYES_WIDE
#define PORT
Definition: cmd_server.hpp:32
data_t genReturnToZero(MartyCore *robot, float period)
Definition: trajectory.cpp:463


ros_marty
Author(s): Alejandro Bordallo
autogenerated on Fri Jul 21 2017 14:51:45