VEXProRangeMotorLoop.cpp
Go to the documentation of this file.
1 /*
2  * VEXProRangeMotorLoop.cpp
3  *
4  * Created on: Jul 13, 2012
5  * Author: bouchier
6  * Publish the range from a Sonar connected to digital 1 (Input) &
7  * digital 2 (Output) on topic sonar1. Control motor 13 speed by
8  * publishing the desired speed on a ros topic with e.g.
9  * $ rostopic pub my_topic std_msgs/Int32 120.
10  * Drives the motor on VEXPro motor13 connection to the requested value: -255 to +255
11  * that is received on subscribed topic motor1
12  *
13  * Note: connector labeled "INPUT" on sonar sensor goes to
14  * digital 1 (bit 0), and connector labeled "OUTPUT" goes to
15  * digital 2 (bit 1).
16  */
17 
18 
19 #include <ros.h>
20 #include <std_msgs/Int32.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <qegpioint.h>
24 #include <qemotoruser.h>
25 
27 std_msgs::Int32 range;
28 ros::Publisher sonar1("sonar1", &range);
29 CQEMotorUser &motor = CQEMotorUser::GetRef(); // motor singleton
30 CQEGpioInt &gpio = CQEGpioInt::GetRef(); // GPIO singleton
31 
32 char *rosSrvrIp = "192.168.11.9";
33 
34 #define USPI 150
35 #define BIAS 300
36 
37 /*
38  * Motor callback - called when new motor speed is published
39  */
40 void motorCb(const std_msgs::Int32& motor13_msg){
41  int speed = motor13_msg.data;
42  printf("Received subscribed motor speed %d\n", speed);
43  motor.SetPWM(0, speed);
44 }
46 
47 /*
48  * Calculate difference in usec between sonar start & end timevals
49  */
50 unsigned long diff(struct timeval *ptv0, struct timeval *ptv1)
51 {
52  long val;
53 
54  val = ptv1->tv_usec - ptv0->tv_usec;
55  val += (ptv1->tv_sec - ptv0->tv_sec)*1000000;
56 
57  return val;
58 }
59 
60 /*
61  * Sonar callback. Called at interrupt level when sonar output transitions,
62  * indicating end of range measurement
63  */
64 void callback(unsigned int io, struct timeval *ptv, void *userdata)
65 {
66  static struct timeval tv0;
67  static int flag = 0;
68  int sonarVal;
69 
70  if (io==0)
71  {
72  flag = 1;
73  tv0 = *ptv;
74  }
75 
76  if (io==1 && flag)
77  {
78  sonarVal = diff(&tv0, ptv);
79  if (sonarVal>BIAS)
80  sonarVal = (sonarVal-BIAS)/USPI;
81  range.data = sonarVal;
82  //printf("%d\n", sonarVal);
83  }
84 }
85 
86 int main()
87 {
88  volatile unsigned int d;
89 
90  /* initialize ROS & subscribers & publishers */
91  //nh.initNode();
92  nh.initNode(rosSrvrIp);
93  nh.advertise(sonar1); // advertise sonar range topic
94  nh.subscribe(motorSub); // subscribe to motor speed topic
95 
96  // reset bit 0, set as output for sonar trigger
97  gpio.SetData(0x0000);
98  gpio.SetDataDirection(0x0001);
99 
100  // set callbacks on negative edge for both bits 0 (trigger)
101  // and 1 (echo)
102  gpio.RegisterCallback(0, NULL, callback);
103  gpio.RegisterCallback(1, NULL, callback);
104  gpio.SetInterruptMode(0, QEG_INTERRUPT_NEGEDGE);
105  gpio.SetInterruptMode(1, QEG_INTERRUPT_NEGEDGE);
106 
107  // trigger sonar by toggling bit 0
108  while(1)
109  {
110  gpio.SetData(0x0001);
111  for (d=0; d<120000; d++);
112  gpio.SetData(0x0000);
113  usleep(100000); // the interrupt breaks us out of this sleep
114  usleep(100000); // now really sleep
115  sonar1.publish( &range );
116  nh.spinOnce();
117  }
118 }
119 
120 
d
CQEMotorUser & motor
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
char * rosSrvrIp
unsigned long diff(struct timeval *ptv0, struct timeval *ptv1)
ros::NodeHandle nh
ros::Publisher sonar1("sonar1",&range)
void motorCb(const std_msgs::Int32 &motor13_msg)
std_msgs::Int32 range
CQEGpioInt & gpio
void callback(unsigned int io, struct timeval *ptv, void *userdata)
ros::Subscriber< std_msgs::Int32 > motorSub("motor13", motorCb)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define USPI
#define BIAS
int main()


rosserial_embeddedlinux
Author(s): Paul Bouchier
autogenerated on Mon Jun 10 2019 14:53:23