Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "mbed.h"
00013 #include "MotorDriver.h"
00014 #include <ros.h>
00015 #include <geometry_msgs/Twist.h>
00016
00017 #ifdef TARGET_LPC1768
00018 #define MOTORSHIELD_IN1 p21
00019 #define MOTORSHIELD_IN2 p22
00020 #define MOTORSHIELD_IN3 p23
00021 #define MOTORSHIELD_IN4 p24
00022 #define SPEEDPIN_A p25
00023 #define SPEEDPIN_B p26
00024 #elif defined(TARGET_KL25Z) || defined(TARGET_NUCLEO_F401RE)
00025 #define MOTORSHIELD_IN1 D8
00026 #define MOTORSHIELD_IN2 D11
00027 #define MOTORSHIELD_IN3 D12
00028 #define MOTORSHIELD_IN4 D13
00029 #define SPEEDPIN_A D9
00030 #define SPEEDPIN_B D10
00031 #else
00032 #error "You need to specify a pin for the sensor"
00033 #endif
00034
00035 MotorDriver motorDriver(MOTORSHIELD_IN1, MOTORSHIELD_IN2, MOTORSHIELD_IN3, MOTORSHIELD_IN4, SPEEDPIN_A, SPEEDPIN_B);
00036 ros::NodeHandle nh;
00037
00038 void messageCb(const geometry_msgs::Twist& msg)
00039 {
00040 if (msg.angular.z == 0 && msg.linear.x == 0)
00041 {
00042 motorDriver.stop();
00043 }
00044 else
00045 {
00046 if (msg.angular.z < 0)
00047 {
00048 int speed = (int)(msg.angular.z * -100);
00049 motorDriver.setSpeed(speed, MOTORA);
00050 motorDriver.setSpeed(speed, MOTORB);
00051 motorDriver.goRight();
00052 }
00053 else if (msg.angular.z > 0)
00054 {
00055 int speed = (int)(msg.angular.z * 100);
00056 motorDriver.setSpeed(speed, MOTORA);
00057 motorDriver.setSpeed(speed, MOTORB);
00058 motorDriver.goLeft();
00059 }
00060 else if (msg.linear.x < 0)
00061 {
00062 int speed = (int)(msg.linear.x * -100);
00063 motorDriver.setSpeed(speed, MOTORA);
00064 motorDriver.setSpeed(speed, MOTORB);
00065 motorDriver.goBackward();
00066 }
00067 else if (msg.linear.x > 0)
00068 {
00069 int speed = (int)(msg.linear.x * 100);
00070 motorDriver.setSpeed(speed, MOTORA);
00071 motorDriver.setSpeed(speed, MOTORB);
00072 motorDriver.goForward();
00073 }
00074 }
00075 }
00076
00077 ros::Subscriber<geometry_msgs::Twist> sub("cmd_vel", &messageCb);
00078 Timer t;
00079
00080 int main()
00081 {
00082 t.start();
00083 long vel_timer = 0;
00084 nh.initNode();
00085 nh.subscribe(sub);
00086 motorDriver.init();
00087 motorDriver.setSpeed(90, MOTORB);
00088 motorDriver.setSpeed(90, MOTORA);
00089 while (1)
00090 {
00091 if (t.read_ms() > vel_timer)
00092 {
00093 motorDriver.stop();
00094 vel_timer = t.read_ms() + 500;
00095 }
00096 nh.spinOnce();
00097 wait_ms(1);
00098 }
00099 }