Go to the documentation of this file.00001
00030 #include <boost/assign.hpp>
00031 #include <ubiquity_motor/motor_hardware.h>
00032 #include <ubiquity_motor/motor_message.h>
00033
00034 #include <boost/math/special_functions/round.hpp>
00035
00036
00037
00038
00039
00040
00041 #define TICS_PER_RADIAN (41.0058030317/2)
00042 #define SECONDS_PER_VELOCITY_READ 10.0 //read = ticks / (100 ms), so we have scale of 10 for ticks/second
00043 #define CURRENT_FIRMWARE_VERSION 18
00044
00045 MotorHardware::MotorHardware(ros::NodeHandle nh){
00046 ros::V_string joint_names = boost::assign::list_of("left_wheel_joint")("right_wheel_joint");
00047
00048 for (unsigned int i = 0; i < joint_names.size(); i++) {
00049 hardware_interface::JointStateHandle joint_state_handle(joint_names[i],
00050 &joints_[i].position, &joints_[i].velocity, &joints_[i].effort);
00051 joint_state_interface_.registerHandle(joint_state_handle);
00052
00053 hardware_interface::JointHandle joint_handle(
00054 joint_state_handle, &joints_[i].velocity_command);
00055 velocity_joint_interface_.registerHandle(joint_handle);
00056 }
00057 registerInterface(&joint_state_interface_);
00058 registerInterface(&velocity_joint_interface_);
00059
00060
00061
00062 std::string sPort;
00063 int sBaud;
00064
00065 double sLoopRate;
00066
00067 if (!nh.getParam("ubiquity_motor/serial_port", sPort))
00068 {
00069 sPort.assign("/dev/ttyS0");
00070 nh.setParam("ubiquity_motor/serial_port", sPort);
00071 }
00072
00073 if (!nh.getParam("ubiquity_motor/serial_baud", sBaud))
00074 {
00075 sBaud = 9600;
00076 nh.setParam("ubiquity_motor/serial_baud", sBaud);
00077 }
00078
00079 if (!nh.getParam("ubiquity_motor/serial_loop_rate", sLoopRate))
00080 {
00081 sLoopRate = 100;
00082 nh.setParam("ubiquity_motor/serial_loop_rate", sLoopRate);
00083 }
00084
00085 motor_serial_ = new MotorSerial(sPort,sBaud,sLoopRate);
00086 }
00087
00088 MotorHardware::~MotorHardware(){
00089 delete motor_serial_;
00090 }
00091
00092 void MotorHardware::readInputs(){
00093 while(motor_serial_->commandAvailable()){
00094 MotorMessage mm;
00095 mm = motor_serial_-> receiveCommand();
00096 if(mm.getType() == MotorMessage::TYPE_RESPONSE){
00097 switch(mm.getRegister()){
00098 case MotorMessage::REG_FIRMWARE_VERSION:
00099 if (mm.getData() != CURRENT_FIRMWARE_VERSION) {
00100 ROS_FATAL("Firmware version %d, expect %d",
00101 mm.getData(), CURRENT_FIRMWARE_VERSION);
00102 }
00103 else {
00104 ROS_INFO("Firmware version %d", mm.getData());
00105 }
00106 break;
00107 case MotorMessage::REG_LEFT_ODOM:
00108 joints_[0].position += mm.getData()/TICS_PER_RADIAN;
00109 break;
00110 case MotorMessage::REG_RIGHT_ODOM:
00111 joints_[1].position += mm.getData()/TICS_PER_RADIAN;
00112 break;
00113 case MotorMessage::REG_LEFT_SPEED_MEASURED:
00114 joints_[0].velocity = mm.getData()*SECONDS_PER_VELOCITY_READ/TICS_PER_RADIAN;
00115 break;
00116 case MotorMessage::REG_RIGHT_SPEED_MEASURED:
00117 joints_[1].velocity = mm.getData()*SECONDS_PER_VELOCITY_READ/TICS_PER_RADIAN;
00118 break;
00119 }
00120 }
00121 }
00122 }
00123
00124 void MotorHardware::writeSpeeds(){
00125 std::vector<MotorMessage> commands;
00126
00127
00128
00129
00130 MotorMessage left_odom;
00131 left_odom.setRegister(MotorMessage::REG_LEFT_ODOM);
00132 left_odom.setType(MotorMessage::TYPE_READ);
00133 left_odom.setData(0);
00134 commands.push_back(left_odom);
00135
00136 MotorMessage right_odom;
00137 right_odom.setRegister(MotorMessage::REG_RIGHT_ODOM);
00138 right_odom.setType(MotorMessage::TYPE_READ);
00139 right_odom.setData(0);
00140 commands.push_back(right_odom);
00141
00142
00143
00144
00145 MotorMessage left_vel;
00146 left_vel.setRegister(MotorMessage::REG_LEFT_SPEED_MEASURED);
00147 left_vel.setType(MotorMessage::TYPE_READ);
00148 left_vel.setData(0);
00149 commands.push_back(left_vel);
00150
00151 MotorMessage right_vel;
00152 right_vel.setRegister(MotorMessage::REG_RIGHT_SPEED_MEASURED);
00153 right_vel.setType(MotorMessage::TYPE_READ);
00154 right_vel.setData(0);
00155 commands.push_back(right_vel);
00156
00157
00158
00159
00160 MotorMessage left;
00161 left.setRegister(MotorMessage::REG_LEFT_SPEED_SET);
00162 left.setType(MotorMessage::TYPE_WRITE);
00163 left.setData(boost::math::lround(joints_[0].velocity_command*TICS_PER_RADIAN/SECONDS_PER_VELOCITY_READ));
00164 commands.push_back(left);
00165
00166 MotorMessage right;
00167 right.setRegister(MotorMessage::REG_RIGHT_SPEED_SET);
00168 right.setType(MotorMessage::TYPE_WRITE);
00169 right.setData(boost::math::lround(joints_[1].velocity_command*TICS_PER_RADIAN/SECONDS_PER_VELOCITY_READ));
00170 commands.push_back(right);
00171
00172
00173
00174 motor_serial_->transmitCommands(commands);
00175
00176
00177
00178 }
00179
00180 void MotorHardware::requestVersion(){
00181 MotorMessage version;
00182 version.setRegister(MotorMessage::REG_FIRMWARE_VERSION);
00183 version.setType(MotorMessage::TYPE_READ);
00184 version.setData(0);
00185 motor_serial_->transmitCommand(version);
00186
00187 }
00188
00189 void MotorHardware::requestOdometry(){
00190
00191 std::vector<MotorMessage> commands;
00192
00193 MotorMessage left_odom;
00194 left_odom.setRegister(MotorMessage::REG_LEFT_ODOM);
00195 left_odom.setType(MotorMessage::TYPE_READ);
00196 left_odom.setData(0);
00197 commands.push_back(left_odom);
00198
00199 MotorMessage right_odom;
00200 right_odom.setRegister(MotorMessage::REG_RIGHT_ODOM);
00201 right_odom.setType(MotorMessage::TYPE_READ);
00202 right_odom.setData(0);
00203 commands.push_back(right_odom);
00204
00205 motor_serial_->transmitCommands(commands);
00206 }
00207
00208 void MotorHardware::requestVelocity(){
00209 std::vector<MotorMessage> commands;
00210
00211 MotorMessage left_vel;
00212 left_vel.setRegister(MotorMessage::REG_LEFT_SPEED_MEASURED);
00213 left_vel.setType(MotorMessage::TYPE_READ);
00214 left_vel.setData(0);
00215 commands.push_back(left_vel);
00216
00217 MotorMessage right_vel;
00218 right_vel.setRegister(MotorMessage::REG_RIGHT_SPEED_MEASURED);
00219 right_vel.setType(MotorMessage::TYPE_READ);
00220 right_vel.setData(0);
00221 commands.push_back(right_vel);
00222
00223 motor_serial_->transmitCommands(commands);
00224 }
00225
00226
00227 void MotorHardware::setPid(int32_t p_set, int32_t i_set, int32_t d_set, int32_t denominator_set){
00228 p_value = p_set;
00229 i_value = i_set;
00230 d_value = d_set;
00231 denominator_value = denominator_set;
00232 }
00233
00234 void MotorHardware::sendPid() {
00235 std::vector<MotorMessage> commands;
00236
00237 MotorMessage p;
00238 p.setRegister(MotorMessage::REG_PARAM_P);
00239 p.setType(MotorMessage::TYPE_WRITE);
00240 p.setData(p_value);
00241 commands.push_back(p);
00242
00243 MotorMessage i;
00244 i.setRegister(MotorMessage::REG_PARAM_I);
00245 i.setType(MotorMessage::TYPE_WRITE);
00246 i.setData(i_value);
00247 commands.push_back(i);
00248
00249 MotorMessage d;
00250 d.setRegister(MotorMessage::REG_PARAM_D);
00251 d.setType(MotorMessage::TYPE_WRITE);
00252 d.setData(d_value);
00253 commands.push_back(d);
00254
00255 MotorMessage denominator;
00256 denominator.setRegister(MotorMessage::REG_PARAM_C);
00257 denominator.setType(MotorMessage::TYPE_WRITE);
00258 denominator.setData(denominator_value);
00259 commands.push_back(denominator);
00260
00261 motor_serial_->transmitCommands(commands);
00262 }