can_interface.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 #include <math.h>
00003 #include "std_msgs/String.h"
00004 #include "quad_can_driver/serial_interface.h"
00005 #include "quad_can_driver/Thrust.h"
00006 #include "quad_can_driver/Attitude.h"
00007 
00008 #define PI 3.14159265359
00009 
00010 struct str_ExternControl ExternControl;
00011 
00012 void ThrustCallback(const quad_can_driver::Thrust& msg){
00013         if(msg.thrust>255){
00014                 ExternControl.Throttle = 255;
00015         }else if(msg.thrust<0){
00016                 ExternControl.Throttle = 0;
00017         }else{
00018                 ExternControl.Throttle = msg.thrust;
00019         }
00020 }
00021 
00022 void ATTCallback(const quad_can_driver::Attitude& msg){
00023         
00024         float att_coef = 128/(PI/4);
00025         float roll_, pitch_;
00026         
00027         pitch_ = round(-msg.pitch*att_coef);
00028         roll_ = round(-msg.roll*att_coef);
00029         
00030         if(pitch_>127){
00031                 pitch_ = 127;
00032         }else if(pitch_<-128){
00033                 pitch_ = -128;
00034         }
00035         
00036         if(roll_>127){
00037                 roll_ = 127;
00038         }else if(roll_<-128){
00039                 roll_ = -128;
00040         }
00041         
00042         ExternControl.Pitch = pitch_;
00043         ExternControl.Roll = roll_;
00044         
00045         //Falta o yaw (saber a que angulos correspondem as entradas)
00046         
00047         //ROS_INFO("I'll send: [%d] Roll & [%d] Pitch", ExternControl.Roll, ExternControl.Pitch);
00048 }
00049 
00050 int main(int argc, char **argv){
00051         
00052         ros::init(argc, argv, "can_interface");
00053         ros::NodeHandle nh, nh_("~");
00054           
00055         ros::Subscriber sub1 = nh.subscribe("ThrustCtrl", 10, ThrustCallback);
00056         ros::Subscriber sub2 = nh.subscribe("AttitudeCtrl", 10, ATTCallback);
00057         
00058         double freq_;
00059         std::string port_;
00060         int speed_ = 115200;
00061         
00062         nh_.param<std::string>("dev_port", port_, "/dev/Can_Converter");
00063         nh_.param("freq", freq_, 100.0); //Hz - send msg periodically (at least once every second)
00064         
00065         if (freq_<1){
00066                 ROS_FATAL("Frequency must be greater than 1Hz");
00067                 return 0;
00068         }
00069                 
00070         ros::Rate loop_rate(freq_);
00071         
00072         InitSerialInterface (port_, speed_);
00073                 
00074         ExternControl.Throttle = 0;     
00075         ExternControl.Yaw = 0;
00076         ExternControl.Pitch = 0;
00077         ExternControl.Roll = 0;
00078         ExternControl.Aux1 = 128;
00079     ExternControl.Aux2 = -128;
00080         ExternControl.Aux3 = 0;
00081         ExternControl.Aux4 = 0;
00082         
00083         while (ros::ok()){
00084                 SendOutData(0x70, ExternControl, 0x08);
00085                 /*ROS_INFO("Throttle: %3d    Yaw: %4d    Pitch: %4d    Roll: %4d",
00086                                                 ExternControl.Throttle, ExternControl.Yaw, ExternControl.Pitch, ExternControl.Roll);*/
00087                 ROS_INFO("Throttle: %3d",ExternControl.Throttle);
00088                 ros::spinOnce();
00089                 loop_rate.sleep();
00090         }
00091         CloseSerialInterface();
00092         return 0;
00093 }


quad_can_driver
Author(s): Henrique
autogenerated on Mon Jan 6 2014 11:48:02