Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "SerialCommandDevice.hpp"
00009
00010 namespace TELEKYB_NAMESPACE {
00011
00012 SerialCommandDevice::SerialCommandDevice()
00013 : SerialDevice(SerialCommandDeviceOptions::Instance().tDeviceName->getValue()),
00014 options(SerialCommandDeviceOptions::Instance())
00015 {
00016 nodeHandle = ROSModule::Instance().getMainNodeHandle();
00017 commandSub = nodeHandle.subscribe(options.tTopicName->getValue(),1, &SerialCommandDevice::commandCallback, this);
00018
00019
00020
00021 setTermiosAttrCFlag(options.tTermiosCFlags->getValue());
00022 setTermiosAttrSpeed(options.tBaudRate->getValue().value(),options.tBaudRate->getValue().value());
00023
00024 }
00025
00026 SerialCommandDevice::~SerialCommandDevice() {
00027
00028 }
00029
00030 void SerialCommandDevice::commandCallback(const telekyb_msgs::TKMotorCommands::ConstPtr& commandMsg)
00031 {
00032 if (secureTimer.getElapsed().toDSec() < options.minTimeStep->getValue()){
00033 ROS_WARN("Ignored command. Receiving command too fast. Maximum allowed command rate is %fHz.",1.0/options.minTimeStep->getValue());
00034 return;
00035 }
00036 secureTimer.reset();
00037
00038
00039
00040 int size = commandMsg->force.size();
00041 if (size == options.motorNumber->getValue()){
00042 unsigned char buf[]={'a',0,0,0,0,'\r'};
00043 for (int motorIndex = 1; motorIndex < size+1; motorIndex++){
00044 buf[motorIndex] = setpoint(commandMsg->force[motorIndex]);
00045 }
00046
00047 try {
00048 writeDevice(buf, sizeof(buf));
00049 } catch (SerialException &e) {
00050 e.process();
00051 }
00052 } else {
00053 ROS_WARN("The number of commands is not the same as the number of motors. Commands ignored!");
00054 }
00055 }
00056
00057 unsigned char SerialCommandDevice::setpoint(double force){
00058 force = std::max(std::min(force,7.0),.3);
00059 double t0 = sqrt(force*7.262570089866418E31-2.029308540566121E31)*(6.4E1/8.063072531723689E15)+1.599972254586687E17/8.063072531723689E15;
00060
00061 return (unsigned char)floor(t0+.5);
00062 }
00063
00064 }
00065