miniQ.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2012, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita on 20/08/2012
00036 *********************************************************************/
00037 
00038 #include <ros/ros.h>
00039 #include "miniQ.h"
00040 
00041 cereal::CerealPort miniQ::serial_port;
00042 
00043 miniQ::miniQ()
00044 {
00045     id_ = 0;
00046     linear_velocity_ = 0.0;
00047     angular_velocity_ = 0.0;
00048 }
00049 
00050 miniQ::~miniQ()
00051 {
00052     
00053 }
00054 
00055 bool miniQ::openPort(char * port, int baudrate)
00056 {
00057     try{ serial_port.open(port, baudrate); }
00058     catch(cereal::Exception& e)
00059     {
00060         return false;
00061     }
00062     return true;
00063 }
00064 
00065 bool miniQ::checkVersion()
00066 {
00067     char msg[MSG_LENGTH];
00068     sprintf(msg, "@%d,%de", id_, MQ_ACTION_GET_VERSION);
00069     
00070     serial_port.write(msg);
00071     
00072     std::string reply;
00073     try{ serial_port.readBetween(&reply, '@', 'e', 100); }
00074     catch(cereal::TimeoutException& e)
00075     {
00076      //   return false;
00077     }
00078     
00079     //ROS_INFO("Reply: %s", reply.c_str());
00080     
00081     int id, action, version;
00082     sscanf(reply.c_str(), "@%d,%d,%de", &id, &action, &version);
00083     
00084     if(version != 3) return false;
00085 
00086     id_ = id;
00087     
00088     return true;
00089 }
00090 
00091 void miniQ::setVelocities(double linear_velocity, double angular_velocity)
00092 {
00093     linear_velocity_= linear_velocity;
00094     angular_velocity_ = angular_velocity;
00095 }
00096 
00097 void miniQ::setPWM(int left_pwm, int left_dir, int right_pwm, int right_dir)
00098 {   
00099     char msg[MSG_LENGTH];
00100     sprintf(msg, "@%d,%d,%d,%d,%d,%de", id_, MQ_ACTION_DRIVE_DIRECT, left_pwm, left_dir, right_pwm, right_dir);
00101     
00102     serial_port.write(msg);
00103 }
00104 
00105 bool miniQ::update()
00106 {
00107     int linear_velocity_int = (int)(linear_velocity_*1000);
00108     int angular_velocity_int = (int)(angular_velocity_*1000);
00109     
00110     char msg[MSG_LENGTH];
00111     sprintf(msg, "@%d,%d,%d,%d,%de", id_, MQ_ACTION_DRIVE, linear_velocity_int, angular_velocity_int, MQ_ACTION_GET_GROUP_1);
00112     
00113     //ROS_INFO("Drive: %s", msg);
00114 
00115     serial_port.write(msg);
00116     
00117     std::string reply;
00118     try{ serial_port.readBetween(&reply, '@', 'e', 50); }
00119     catch(cereal::TimeoutException& e)
00120     {
00121         return false;
00122     }
00123     
00124     int id, action, x, y, yaw, gas, gas_raw;
00125     sscanf(reply.c_str(), "@%d,%d,%d,%d,%d,%d,%de", &id, &action, &x, &y, &yaw, &gas, &gas_raw);
00126     
00127     x_ = x/1000.0;
00128     y_ = y/1000.0;
00129     yaw_ = yaw/1000.0;
00130 
00131     double normalized_gas = (gas_raw-5)/500.0;
00132     if(normalized_gas < 0.0) normalized_gas = 0.0;
00133     else if(normalized_gas > 1.0) normalized_gas = 1.0;
00134     gas_ = normalized_gas;
00135     gas_raw_ = gas_raw;
00136     
00137     return true;
00138 }
00139 
00140 bool miniQ::updateVelocities()
00141 {
00142     int linear_velocity_int = (int)(linear_velocity_*1000);
00143     int angular_velocity_int = (int)(angular_velocity_*1000);
00144     
00145     char msg[MSG_LENGTH];
00146     sprintf(msg, "@%d,%d,%d,%d,%de", id_, MQ_ACTION_DRIVE, linear_velocity_int, angular_velocity_int, 0);
00147     
00148     //ROS_INFO("Drive: %s", msg);
00149     
00150     serial_port.write(msg);
00151     
00152     return true;
00153 }
00154 
00155 void miniQ::setId(int id)
00156 {
00157     id_ = id;
00158 }
00159 
00160 int miniQ::scanForId(int id)
00161 {
00162     char msg[MSG_LENGTH];
00163     sprintf(msg, "@%d,%de", id, MQ_ACTION_GET_VERSION);
00164     
00165     serial_port.write(msg);
00166     
00167     std::string reply;
00168     try{ serial_port.readBetween(&reply, '@', 'e', 100); }
00169     catch(cereal::TimeoutException& e)
00170     {
00171         return 0;
00172     }
00173     
00174     int robot_id, action, version, calibrated;
00175     sscanf(reply.c_str(), "@%d,%d,%d,%de", &robot_id, &action, &version, &calibrated);
00176     
00177     if(version > 0) return id;
00178     return 0;
00179 }
00180 
00181 bool miniQ::setMode(int mode)
00182 {
00183     char msg[MSG_LENGTH];
00184     sprintf(msg, "@%d,%d,%de", id_, MQ_ACTION_SET_MODE, mode);
00185     
00186     serial_port.write(msg);
00187 
00188     return true;
00189 }
00190 
00191 bool miniQ::setBatteryType(int battery_type)
00192 {
00193     char msg[MSG_LENGTH];
00194     sprintf(msg, "@%d,%d,%de", id_, MQ_ACTION_SET_BATTERY_TYPE, battery_type);
00195     
00196     serial_port.write(msg);
00197 
00198     return true;
00199 }
00200 
00201 bool miniQ::setPIDGains(int kp, int ki, int kd, miniQPID pid)
00202 {
00203     char msg[MSG_LENGTH];
00204     
00205     int side, stage;
00206     if(pid == LeftStartingPID)
00207     {
00208         side = 0;
00209         stage = 0;
00210     }
00211     else if(pid == LeftRunningPID)
00212     {
00213         side = 0;
00214         stage = 1;
00215     }
00216     else if(pid == RightStartingPID)
00217     {
00218         side = 1;
00219         stage = 0;
00220     }
00221     else if(pid == RightRunningPID)
00222     {
00223         side = 1;
00224         stage = 1;
00225     }
00226     
00227     sprintf(msg, "@%d,%d,%d,%d,%d,%d,%de", id_, MQ_ACTION_SET_PID_GAINS, side, stage, kp, ki, kd);
00228     
00229     serial_port.write(msg);
00230 
00231     return true;        
00232 }
00233 
00234 bool miniQ::setOdometryCallibration(double odometry_d, double odometry_yaw)
00235 {
00236     char msg[MSG_LENGTH];
00237     sprintf(msg, "@%d,%d,%d,%de", id_, MQ_ACTION_SET_ODOMETRY_CALIBRATION, (int)(odometry_d*1000), (int)(odometry_yaw*1000));
00238     
00239     serial_port.write(msg);
00240 
00241     return true;
00242 }
00243 
00244 bool miniQ::setGasSensorCallibration(double a, double b)
00245 {
00246     char msg[MSG_LENGTH];
00247     sprintf(msg, "@%d,%d,%d,%de", id_, MQ_ACTION_SET_GAS_CALIBRATION, (int)(a*1000), (int)(b*1000));
00248     
00249     serial_port.write(msg);
00250 
00251     return true;
00252 }
00253 
00254 bool miniQ::setTimeout(double timeout)
00255 {
00256     char msg[MSG_LENGTH];
00257     sprintf(msg, "@%d,%d,%de", id_, MQ_ACTION_SET_TIMEOUT, (int)(timeout*1000));
00258     
00259     serial_port.write(msg);
00260     
00261     return true;
00262 }
00263 
00264 bool miniQ::updateWheelVelocities()
00265 {
00266     char msg[MSG_LENGTH];
00267     sprintf(msg, "@%d,%de",  id_, MQ_ACTION_GET_WHEEL_VELOCITIES);
00268     
00269     serial_port.write(msg);
00270     
00271     std::string reply;
00272     try{ serial_port.readBetween(&reply, '@', 'e', 50); }
00273     catch(cereal::TimeoutException& e)
00274     {
00275         return false;
00276     }
00277     
00278     int id, action, l, r;
00279     sscanf(reply.c_str(), "@%d,%d,%d,%de", &id, &action, &l, &r);
00280     
00281     left_wheel_velocity_ = l/1000.0;
00282     right_wheel_velocity_ = r/1000.0;
00283     
00284     return true;
00285 }
00286 
00287 // EOF


lse_miniq_driver
Author(s): Gonçalo Cabrita
autogenerated on Mon Jan 6 2014 11:25:52