MotorShield.cpp
Go to the documentation of this file.
1 /*
2  * rosserial Motor Shield Example
3  *
4  * This tutorial demonstrates the usage of the
5  * Seeedstudio Motor Shield
6  * http://www.seeedstudio.com/depot/motor-shield-v20-p-1377.html
7  *
8  * Source Code Based on:
9  * https://developer.mbed.org/teams/shields/code/Seeed_Motor_Shield/
10  */
11 
12 #include "mbed.h"
13 #include "MotorDriver.h"
14 #include <ros.h>
15 #include <geometry_msgs/Twist.h>
16 
17 #ifdef TARGET_LPC1768
18 #define MOTORSHIELD_IN1 p21
19 #define MOTORSHIELD_IN2 p22
20 #define MOTORSHIELD_IN3 p23
21 #define MOTORSHIELD_IN4 p24
22 #define SPEEDPIN_A p25
23 #define SPEEDPIN_B p26
24 #elif defined(TARGET_KL25Z) || defined(TARGET_NUCLEO_F401RE)
25 #define MOTORSHIELD_IN1 D8
26 #define MOTORSHIELD_IN2 D11
27 #define MOTORSHIELD_IN3 D12
28 #define MOTORSHIELD_IN4 D13
29 #define SPEEDPIN_A D9
30 #define SPEEDPIN_B D10
31 #else
32 #error "You need to specify a pin for the sensor"
33 #endif
34 
35 MotorDriver motorDriver(MOTORSHIELD_IN1, MOTORSHIELD_IN2, MOTORSHIELD_IN3, MOTORSHIELD_IN4, SPEEDPIN_A, SPEEDPIN_B);
37 
38 void messageCb(const geometry_msgs::Twist& msg)
39 {
40  if (msg.angular.z == 0 && msg.linear.x == 0)
41  {
42  motorDriver.stop();
43  }
44  else
45  {
46  if (msg.angular.z < 0)
47  {
48  int speed = (int)(msg.angular.z * -100);
49  motorDriver.setSpeed(speed, MOTORA);
50  motorDriver.setSpeed(speed, MOTORB);
52  }
53  else if (msg.angular.z > 0)
54  {
55  int speed = (int)(msg.angular.z * 100);
56  motorDriver.setSpeed(speed, MOTORA);
57  motorDriver.setSpeed(speed, MOTORB);
59  }
60  else if (msg.linear.x < 0)
61  {
62  int speed = (int)(msg.linear.x * -100);
63  motorDriver.setSpeed(speed, MOTORA);
64  motorDriver.setSpeed(speed, MOTORB);
66  }
67  else if (msg.linear.x > 0)
68  {
69  int speed = (int)(msg.linear.x * 100);
70  motorDriver.setSpeed(speed, MOTORA);
71  motorDriver.setSpeed(speed, MOTORB);
73  }
74  }
75 }
76 
78 Timer t;
79 
80 int main()
81 {
82  t.start();
83  long vel_timer = 0;
84  nh.initNode();
85  nh.subscribe(sub);
86  motorDriver.init();
89  while (1)
90  {
91  if (t.read_ms() > vel_timer)
92  {
93  motorDriver.stop();
94  vel_timer = t.read_ms() + 500;
95  }
96  nh.spinOnce();
97  wait_ms(1);
98  }
99 }
ros::Subscriber< geometry_msgs::Twist > sub("cmd_vel",&messageCb)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
Timer t
Definition: MotorShield.cpp:78
void goRight()
Definition: MotorDriver.cpp:97
#define MOTORB
Definition: MotorDriver.h:30
void messageCb(const geometry_msgs::Twist &msg)
Definition: MotorShield.cpp:38
void goBackward()
Definition: MotorDriver.cpp:87
void setSpeed(uint8_t speed, uint8_t motorID)
Definition: MotorDriver.cpp:43
#define MOTORA
Definition: MotorDriver.h:29
void goLeft()
Definition: MotorDriver.cpp:92
void goForward()
Definition: MotorDriver.cpp:82
MotorDriver motorDriver(MOTORSHIELD_IN1, MOTORSHIELD_IN2, MOTORSHIELD_IN3, MOTORSHIELD_IN4, SPEEDPIN_A, SPEEDPIN_B)
ros::NodeHandle nh
Definition: MotorShield.cpp:36
int main()
Definition: MotorShield.cpp:80


rosserial_mbed
Author(s): Gary Servin
autogenerated on Mon Jun 10 2019 14:53:26