test_pid.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include "roch_base/roch_hardware.h"
00003 
00004 #include <boost/assign/list_of.hpp>
00005 #include <float.h>
00006 #define polling_timeout_ 20
00007 using namespace std;
00008 namespace
00009 {
00010   const uint8_t LEFT = 0, RIGHT = 1;
00011 };
00012 
00013 double speed_x;
00014 double speed_z;
00015 void publishRawData()
00016 {
00017 #if 0
00018         std::ostringstream ostream;
00019       sawyer::base_data::RawData data =sawyer::Transport::instance().getdata();
00020       ostream << " { " ;
00021       ostream << std::setfill('0') << std::uppercase;
00022       for (unsigned int i=0; i < data.length; i++)
00023           ostream << std::hex << std::setw(2) << static_cast<unsigned int>(data.data[i]) << " " << std::dec;
00024       ostream << "}";
00025       std_msgs::StringPtr msg(new std_msgs::String);
00026       msg->data = ostream.str();
00027       ROS_INFO_STREAM("reveive command:"<<msg->data.c_str()<<" .");
00028 #endif
00029 }
00030 
00031 void controloverallSpeed(double lin_vel, double ang_vel, double accel_left, double accel_right)
00032   {
00033     bool success = false;
00034     
00035     double acc = (accel_left + accel_right)*0.5;
00036     
00037     while (!success)
00038     {
00039       try
00040       {
00041 //      sawyer::SetVelocity(lin_vel,ang_vel,acc).send();
00042 //    sawyer::SetDifferentialControl(0.1,0.0005,0.08,0.1,0.0005,0.08).send();
00043       ROS_INFO_STREAM("Now Left_PID: "<<lin_vel<<", Right_PID: "<<ang_vel<<" .");
00044       sawyer::SetDifferentialControl(lin_vel,lin_vel,lin_vel,ang_vel,ang_vel,ang_vel).send();
00045         success = true;
00046       }
00047       catch (sawyer::Exception *ex)
00048       {
00049         ROS_ERROR_STREAM("Error sending Diff Contrl contets command: " << ex->message);
00050        
00051       }
00052     }
00053   }
00054 void speedCallBack(const geometry_msgs::Twist::ConstPtr& speed){
00055   
00056     //cout<<"setspeed "<<speed_x<<"  "<<speed_z<<endl;
00057   speed_x = speed->linear.x;
00058   speed_z = speed->angular.z;
00059   
00060   
00061     controloverallSpeed(speed_x, speed_z, 0, 0);   
00062       
00063     publishRawData();
00064   core::Channel<sawyer::DataDifferentialControl>::Ptr pidData = core::Channel<sawyer::DataDifferentialControl>::requestData(
00065      polling_timeout_);
00066    publishRawData();
00067      if(pidData){
00068       ROS_INFO_STREAM("Received  PID data information, Left_P: "<<pidData->getLeftP()<<", Left_I: "<<pidData->getLeftI()<<", Left_D: "<<pidData->getLeftD()<<"||| Right_P: "<<pidData->getRightP()<<", Right_I: "<<pidData->getRightI()<<", Right_D: "<<pidData->getRightD()<<" .");    
00069       
00070     }
00071     else{
00072        ROS_ERROR("Could not get PID Data form MCU.");
00073     }
00074 #if 0
00075     core::Channel<sawyer::DataVelocity>::Ptr overallspeed = core::Channel<sawyer::DataVelocity>::requestData(
00076       polling_timeout_);
00077       publishRawData();
00078     if (overallspeed)
00079     {
00080       ROS_DEBUG_STREAM("Received  speed information (speed:" << overallspeed->getTranslational() << " Rotational:" << overallspeed->getRotational()<<" Acceleration:"<<overallspeed->getTransAccel() << ")");
00081 
00082       ROS_INFO("Received  speed information,  speed:%.2lf, Rotational:%.2lf, Acceleration:%.2lf",overallspeed->getTranslational(),overallspeed->getRotational(),overallspeed->getTransAccel());     
00083 
00084     }
00085 #endif
00086 }
00087 ros::Subscriber speedSubscriber_;
00088 int main(int argc, char** argv){
00089   
00090   ros::init(argc,argv,"test_imu");
00091   std::string port = "/dev/roch";
00092   core::connect(port);
00093   
00094   ros::NodeHandle nh;
00095   speedSubscriber_ = nh.subscribe("/twist_mux/keyboard_teleop/cmd_vel",100000,&speedCallBack);
00096 
00097     ROS_INFO("begin send requestData");
00098  
00099    ros::spin();
00100   return 0;
00101 }


roch_base
Author(s): Mike Purvis , Paul Bovbel , Carl
autogenerated on Sat Jun 8 2019 20:32:33