SerialCommandDevice.cpp
Go to the documentation of this file.
00001 /*
00002  * SerialCommandDevice.cpp
00003  *
00004  *  Created on: Jul 11, 2012
00005  *      Author: mriedel
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         // Configure Connection
00020         //std::cout << "BaudRate: " << options.tBaudRate->getValue().value() << std::endl;
00021         setTermiosAttrCFlag(options.tTermiosCFlags->getValue());
00022         setTermiosAttrSpeed(options.tBaudRate->getValue().value(),options.tBaudRate->getValue().value());
00023 
00024 }
00025 
00026 SerialCommandDevice::~SerialCommandDevice() {
00027         // TODO Auto-generated destructor stub
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         // check size
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                 //ROS_INFO("Writing %x%x%x%x%x%x on serial",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
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); //saturate before transforming
00059         double t0 = sqrt(force*7.262570089866418E31-2.029308540566121E31)*(6.4E1/8.063072531723689E15)+1.599972254586687E17/8.063072531723689E15;
00060         //t0 = std::max(std::min(t0,180.0),30.0);
00061         return (unsigned char)floor(t0+.5); //round to closest intefer!
00062 }
00063 
00064 } /* TELEKYB_NAMESPACE */
00065 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


tk_smurf_interface
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:14:12