├── .gitignore
├── README.md
├── ros_arduino_bridge
├── CMakeLists.txt
└── package.xml
├── ros_arduino_firmware
├── CMakeLists.txt
├── package.xml
└── src
│ └── libraries
│ ├── MegaRobogaiaPololu
│ ├── MegaRobogaiaPololu.ino
│ ├── commands.h
│ ├── diff_controller.h
│ ├── sensors.h
│ └── servos.h
│ └── ROSArduinoBridge
│ ├── ROSArduinoBridge.ino
│ ├── commands.h
│ ├── diff_controller.h
│ ├── encoder_driver.h
│ ├── encoder_driver.ino
│ ├── motor_driver.h
│ ├── motor_driver.ino
│ ├── sensors.h
│ ├── servos.h
│ └── servos.ino
├── ros_arduino_msgs
├── CMakeLists.txt
├── msg
│ ├── Analog.msg
│ ├── AnalogFloat.msg
│ ├── ArduinoConstants.msg
│ ├── Digital.msg
│ └── SensorState.msg
├── package.xml
└── srv
│ ├── AnalogRead.srv
│ ├── AnalogWrite.srv
│ ├── DigitalRead.srv
│ ├── DigitalSetDirection.srv
│ ├── DigitalWrite.srv
│ ├── ServoRead.srv
│ └── ServoWrite.srv
└── ros_arduino_python
├── CMakeLists.txt
├── config
└── arduino_params.yaml
├── launch
└── arduino.launch
├── nodes
└── arduino_node.py
├── package.xml
├── setup.py
└── src
└── ros_arduino_python
├── __init__.py
├── arduino_driver.py
├── arduino_sensors.py
└── base_controller.py
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # Created by https://www.gitignore.io/api/python
3 |
4 | ### Python ###
5 | # Byte-compiled / optimized / DLL files
6 | __pycache__/
7 | *.py[cod]
8 | *$py.class
9 |
10 | # C extensions
11 | *.so
12 |
13 | # Distribution / packaging
14 | .Python
15 | env/
16 | build/
17 | develop-eggs/
18 | dist/
19 | downloads/
20 | eggs/
21 | .eggs/
22 | lib/
23 | lib64/
24 | parts/
25 | sdist/
26 | var/
27 | wheels/
28 | *.egg-info/
29 | .installed.cfg
30 | *.egg
31 |
32 | # PyInstaller
33 | # Usually these files are written by a python script from a template
34 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
35 | *.manifest
36 | *.spec
37 |
38 | # Installer logs
39 | pip-log.txt
40 | pip-delete-this-directory.txt
41 |
42 | # Unit test / coverage reports
43 | htmlcov/
44 | .tox/
45 | .coverage
46 | .coverage.*
47 | .cache
48 | nosetests.xml
49 | coverage.xml
50 | *,cover
51 | .hypothesis/
52 |
53 | # Translations
54 | *.mo
55 | *.pot
56 |
57 | # Django stuff:
58 | *.log
59 | local_settings.py
60 |
61 | # Flask stuff:
62 | instance/
63 | .webassets-cache
64 |
65 | # Scrapy stuff:
66 | .scrapy
67 |
68 | # Sphinx documentation
69 | docs/_build/
70 |
71 | # PyBuilder
72 | target/
73 |
74 | # Jupyter Notebook
75 | .ipynb_checkpoints
76 |
77 | # pyenv
78 | .python-version
79 |
80 | # celery beat schedule file
81 | celerybeat-schedule
82 |
83 | # SageMath parsed files
84 | *.sage.py
85 |
86 | # dotenv
87 | .env
88 |
89 | # virtualenv
90 | .venv
91 | venv/
92 | ENV/
93 |
94 | # Spyder project settings
95 | .spyderproject
96 | .spyproject
97 |
98 | # Rope project settings
99 | .ropeproject
100 |
101 | # mkdocs documentation
102 | /site
103 |
104 | # End of https://www.gitignore.io/api/python
105 | # temporary files
106 | *~
107 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Overview
2 | --------
3 | This branch (indigo-devel) is intended for ROS Indigo and above, and uses the Catkin buildsystem. It may also be compatible with ROS Hydro.
4 |
5 | This ROS stack includes an Arduino library (called ROSArduinoBridge) and a collection of ROS packages for controlling an Arduino-based robot using standard ROS messages and services. The stack does **not** depend on ROS Serial.
6 |
7 | Features of the stack include:
8 |
9 | * Direct support for Ping sonar and Sharp infrared (GP2D12) sensors
10 |
11 | * Can also read data from generic analog and digital sensors
12 |
13 | * Can control digital outputs (e.g. turn a switch or LED on and off)
14 |
15 | * Support for PWM servos
16 | * Configurable base controller if using the required hardware
17 |
18 | The stack includes a base controller for a differential drive
19 | robot that accepts ROS Twist messages and publishes odometry data back to
20 | the PC. The base controller requires the use of a motor controller and encoders for reading odometry data. The current version of the stack provides support for the following base controller hardware:
21 |
22 | * Pololu VNH5019 dual motor controller shield (http://www.pololu.com/catalog/product/2502) or Pololu MC33926 dual motor shield (http://www.pololu.com/catalog/product/2503).
23 |
24 | * Robogaia Mega Encoder shield
25 | (http://www.robogaia.com/two-axis-encoder-counter-mega-shield-version-2.html) or on-board wheel encoder counters.
26 |
27 | **NOTE:** The Robogaia Mega Encoder shield can only be used with an Arduino Mega. The on-board wheel encoder counters are currently only supported by Arduino Uno.
28 |
29 | * L298 motor driver
30 |
31 | * The library can be easily extended to include support for other motor controllers and encoder hardware or libraries.
32 |
33 | Official ROS Documentation
34 | --------------------------
35 | A standard ROS-style version of this documentation can be found on the ROS wiki at:
36 |
37 | http://www.ros.org/wiki/ros_arduino_bridge
38 |
39 |
40 | System Requirements
41 | -------------------
42 | **Python Serial:** To install the python-serial package under Ubuntu, use the command:
43 |
44 | $ sudo apt-get install python-serial
45 |
46 | On non-Ubuntu systems, use either:
47 |
48 | $ sudo pip install --upgrade pyserial
49 |
50 | or
51 |
52 | $ sudo easy_install -U pyserial
53 |
54 | **Arduino IDE 1.6.6 or Higher:**
55 | Note that the preprocessing of conditional #include statements is broken in earlier versions of the Arduino IDE. To ensure that the ROS Arduino Bridge firmware compiles correctly, be sure to install version 1.6.6 or higher of the Arduino IDE. You can download the IDE from https://www.arduino.cc/en/Main/Software.
56 |
57 | **Hardware:**
58 | The firmware should work with any Arduino-compatible controller for reading sensors and controlling PWM servos. However, to use the base controller, you will need a supported motor controller and encoder hardware as described above. If you do not have this hardware, you can still try the package for reading sensors and controlling servos. See the NOTES section at the end of this document for instructions on how to do this.
59 |
60 | To use the base controller you must also install the appropriate libraries for your motor controller and encoders. For the Pololu VNH5019 Dual Motor Shield, the library can be found at:
61 |
62 | https://github.com/pololu/Dual-VNH5019-Motor-Shield
63 |
64 | For the Pololu MC33926 Dual Motor Shield, the library can be found at:
65 |
66 | https://github.com/pololu/dual-mc33926-motor-shield
67 |
68 | The Robogaia Mega Encoder library can be found at:
69 |
70 | http://www.robogaia.com/uploads/6/8/0/9/6809982/__megaencodercounter-1.3.tar.gz
71 |
72 | L298 Motor Driver doesn't require any libraries
73 |
74 | These libraries should be installed in your standard Arduino
75 | sketchbook/libraries directory.
76 |
77 | Finally, it is assumed you are using version 1.0 or greater of the
78 | Arduino IDE.
79 |
80 | Preparing your Serial Port under Linux
81 | --------------------------------------
82 | Your Arduino will likely connect to your Linux computer as port /dev/ttyACM# or /dev/ttyUSB# where # is a number like 0, 1, 2, etc., depending on how many other devices are connected. The easiest way to make the determination is to unplug all other USB devices, plug in your Arduino, then run the command:
83 |
84 | $ ls /dev/ttyACM*
85 |
86 | or
87 |
88 | $ ls /dev/ttyUSB*
89 |
90 | Hopefully, one of these two commands will return the result you're looking for (e.g. /dev/ttyACM0) and the other will return the error "No such file or directory".
91 |
92 | Next you need to make sure you have read/write access to the port. Assuming your Arduino is connected on /dev/ttyACM0, run the command:
93 |
94 | $ ls -l /dev/ttyACM0
95 |
96 | and you should see an output similar to the following:
97 |
98 | crw-rw---- 1 root dialout 166, 0 2013-02-24 08:31 /dev/ttyACM0
99 |
100 | Note that only root and the "dialout" group have read/write access. Therefore, you need to be a member of the dialout group. You only have to do this once and it should then work for all USB devices you plug in later on.
101 |
102 | To add yourself to the dialout group, run the command:
103 |
104 | $ sudo usermod -a -G dialout your_user_name
105 |
106 | where your\_user\_name is your Linux login name. You will likely have to log out of your X-window session then log in again, or simply reboot your machine if you want to be sure.
107 |
108 | When you log back in again, try the command:
109 |
110 | $ groups
111 |
112 | and you should see a list of groups you belong to including dialout.
113 |
114 | Installation of the ros\_arduino\_bridge Stack
115 | ----------------------------------------------
116 |
117 | $ cd ~/catkin_workspace/src
118 | $ git clone https://github.com/hbrobotics/ros_arduino_bridge.git
119 | $ cd ~/catkin_workspace
120 | $ catkin_make
121 |
122 | The provided Arduino library is called ROSArduinoBridge and is
123 | located in the ros\_arduino\_firmware package. This sketch is
124 | specific to the hardware requirements above but it can also be used
125 | with other Arduino-type boards (e.g. Uno) by turning off the base
126 | controller as described in the NOTES section at the end of this
127 | document.
128 |
129 | To install the ROSArduinoBridge library, follow these steps:
130 |
131 | $ cd SKETCHBOOK_PATH
132 |
133 | where SKETCHBOOK_PATH is the path to your Arduino sketchbook directory.
134 |
135 | $ cp -rp `rospack find ros_arduino_firmware`/src/libraries/ROSArduinoBridge ROSArduinoBridge
136 |
137 | This last command copies the ROSArduinoBridge sketch files into your sketchbook folder. The next section describes how to configure, compile and upload this sketch.
138 |
139 |
140 | Loading the ROSArduinoBridge Sketch
141 | -----------------------------------
142 |
143 | * If you are using the base controller, make sure you have already installed the appropriate motor controller and encoder libraries into your Arduino sketchbook/librariesfolder.
144 |
145 | * Launch the Arduino 1.0 IDE and load the ROSArduinoBridge sketch.
146 | You should be able to find it by going to:
147 |
148 | File->Sketchbook->ROSArduinoBridge
149 |
150 | NOTE: If you don't have the required base controller hardware but
151 | still want to try the code, see the notes at the end of the file.
152 |
153 | Choose one of the supported motor controllers by uncommenting its #define statement and commenting out any others. By default, the Pololu VNH5019 driver is chosen.
154 |
155 | Choose a supported encoder library by by uncommenting its #define statement and commenting out any others. the Robogaia Mega Encoder shield is chosen by default.
156 |
157 | If you want to control PWM servos attached to your controller, look for the line:
158 |
159 |
160 | #define USE_SERVOS
161 |
162 |
163 | and make sure it is not commented out like this:
164 |
165 |
166 | //#define USE_SERVOS
167 |
168 |
169 | You must then edit the include file servos.h and change the N_SERVOS
170 | parameter as well as the pin numbers for the servos you have attached.
171 |
172 | * Compile and upload the sketch to your Arduino.
173 |
174 | Firmware Commands
175 | -----------------
176 | The ROSArduinoLibrary accepts single-letter commands over the serial port for polling sensors, controlling servos, driving the robot, and reading encoders. These commands can be sent to the Arduino over any serial interface, including the Serial Monitor in the Arduino IDE.
177 |
178 | **NOTE:** Before trying these commands, set the Serial Monitor baudrate to 57600 and the line terminator to "Carriage return" or "Both NL & CR" using the two pulldown menus on the lower right of the Serial Monitor window.
179 |
180 | The list of commands can be found in the file commands.h. The current list includes:
181 |
182 |
183 | #define ANALOG_READ 'a'
184 | #define GET_BAUDRATE 'b'
185 | #define PIN_MODE 'c'
186 | #define DIGITAL_READ 'd'
187 | #define READ_ENCODERS 'e'
188 | #define MOTOR_SPEEDS 'm'
189 | #define PING 'p'
190 | #define RESET_ENCODERS 'r'
191 | #define SERVO_WRITE 's'
192 | #define SERVO_READ 't'
193 | #define UPDATE_PID 'u'
194 | #define DIGITAL_WRITE 'w'
195 | #define ANALOG_WRITE 'x'
196 |
197 |
198 | For example, to get the analog reading on pin 3, use the command:
199 |
200 | a 3
201 |
202 | To change the mode of digital pin 3 to OUTPUT, send the command:
203 |
204 | c 3 1
205 |
206 | To get the current encoder counts:
207 |
208 | e
209 |
210 | To move the robot forward at 20 encoder ticks per second:
211 |
212 | m 20 20
213 |
214 |
215 | Testing your Wiring Connections
216 | -------------------------------
217 | On a differential drive robot, the motors are connected to the motor controller terminals with opposite polarities to each other. Similarly, the A/B leads from the encoders are connected in the reverse sense to each other. However, you still need to make sure that (a) the wheels move forward when given a positive motor speed and (b) that the encoder counts increase when the wheels move forward.
218 |
219 | After **placing your robot on blocks**, you can use the Serial Monitor in the Arduino IDE to test both requirements. Use the 'm' command to activate the motors, the 'e' command to get the encoder counts, and the 'r' command to reset the encoders to 0. Remember that at the firmware level, motor speeds are given in encoder ticks per second so that for an encoder resolution of, say 4000 counts per wheel revolution, a command such as 'm 20 20' should move the wheels fairly slowly. (The wheels will only move for 2 seconds which is the default setting for the AUTO\_STOP\_INTERVAL.) Also remember that the first argument is the left motor speed and the second argument is the right motor speed. Similarly, when using the 'e' command, the first number returned is the left encoder count and the second number is the right encoder count.
220 |
221 | Finally, you can use the 'r' and 'e' commands to verify the expected encoder counts by rotating the wheels by hand roughly one full turn and checking the reported counts.
222 |
223 |
224 | Configuring the ros\_arduino\_python Node
225 | -----------------------------------------
226 | Now that your Arduino is running the required sketch, you can
227 | configure the ROS side of things on your PC. You define your robot's
228 | dimensions, PID parameters, and sensor configuration by editing the
229 | YAML file in the directory ros\_arduino\_python/config. So first move
230 | into that directory:
231 |
232 | $ roscd ros_arduino_python/config
233 |
234 | Now copy the provided config file to one you can modify:
235 |
236 | $ cp arduino_params.yaml my_arduino_params.yaml
237 |
238 | Bring up your copy of the params file (my\_arduino\_params.yaml) in
239 | your favorite text editor. It should start off looking like this:
240 |
241 |
242 | port: /dev/ttyUSB0
243 | baud: 57600
244 | timeout: 0.1
245 |
246 | rate: 50
247 | sensorstate_rate: 10
248 |
249 | use_base_controller: False
250 | base_controller_rate: 10
251 |
252 | # === Robot drivetrain parameters
253 | #wheel_diameter: 0.146
254 | #wheel_track: 0.2969
255 | #encoder_resolution: 8384 # from Pololu for 131:1 motors
256 | #gear_reduction: 1.0
257 | #motors_reversed: True
258 |
259 | # === PID parameters
260 | #Kp: 20
261 | #Kd: 12
262 | #Ki: 0
263 | #Ko: 50
264 | #accel_limit: 1.0
265 |
266 | # === Sensor definitions. Examples only - edit for your robot.
267 | # Sensor type can be one of the follow (case sensitive!):
268 | # * Ping
269 | # * GP2D12
270 | # * Analog
271 | # * Digital
272 | # * PololuMotorCurrent
273 | # * PhidgetsVoltage
274 | # * PhidgetsCurrent (20 Amp, DC)
275 |
276 | sensors: {
277 | #motor_current_left: {pin: 0, type: PololuMotorCurrent, rate: 5},
278 | #motor_current_right: {pin: 1, type: PololuMotorCurrent, rate: 5},
279 | #ir_front_center: {pin: 2, type: GP2D12, rate: 10},
280 | #sonar_front_center: {pin: 5, type: Ping, rate: 10},
281 | arduino_led: {pin: 13, type: Digital, rate: 5, direction: output}
282 | }
283 |
284 |
285 | **NOTE**: Do not use tabs in your .yaml file or the parser will barf it back out when it tries to load it. Always use spaces instead. **ALSO**: When defining your sensor parameters, the last sensor in the list does **not** get a comma (,) at the end of the line but all the rest **must** have a comma.
286 |
287 | Let's now look at each section of this file.
288 |
289 | _Port Settings_
290 |
291 | The port will likely be either /dev/ttyACM0 or /dev/ttyUSB0. Set accordingly.
292 |
293 | The MegaRobogaiaPololu Arudino sketch connects at 57600 baud by default.
294 |
295 | _Polling Rates_
296 |
297 | The main *rate* parameter (50 Hz by default) determines how fast the
298 | outside ROS loop runs. The default should suffice in most cases. In
299 | any event, it should be at least as fast as your fastest sensor rate
300 | (defined below).
301 |
302 | The *sensorstate\_rate* determines how often to publish an aggregated
303 | list of all sensor readings. Each sensor also publishes on its own
304 | topic and rate.
305 |
306 | The *use\_base\_controller* parameter is set to False by default. Set it to True to use base control (assuming you have the required hardware.) You will also have to set the PID paramters that follow.
307 |
308 | The *base\_controller\_rate* determines how often to publish odometry readings.
309 |
310 | _Defining Sensors_
311 |
312 | The *sensors* parameter defines a dictionary of sensor names and
313 | sensor parameters. (You can name each sensor whatever you like but
314 | remember that the name for a sensor will also become the topic name
315 | for that sensor.)
316 |
317 | The four most important parameters are *pin*, *type*, *rate* and *direction*.
318 | The *rate* defines how many times per second you want to poll that
319 | sensor. For example, a voltage sensor might only be polled once a
320 | second (or even once every 2 seconds: rate=0.5), whereas a sonar
321 | sensor might be polled at 20 times per second. The *type* must be one
322 | of those listed (case sensitive!). The default *direction* is input so
323 | to define an output pin, set the direction explicitly to output. In
324 | the example above, the Arduino LED (pin 13) will be turned on and off
325 | at a rate of 2 times per second.
326 |
327 | _Setting Drivetrain and PID Parameters_
328 |
329 | To use the base controller, you will have to uncomment and set the
330 | robot drivetrain and PID parameters. The sample drivetrain parameters
331 | are for 6" drive wheels that are 11.5" apart. Note that ROS uses
332 | meters for distance so convert accordingly. The sample encoder
333 | resolution (ticks per revolution) is from the specs for the Pololu
334 | 131:1 motor. Set the appropriate number for your motor/encoder
335 | combination. Set the motors_reversed to True if you find your wheels
336 | are turning backward, otherwise set to False.
337 |
338 | The PID parameters are trickier to set. You can start with the sample
339 | values but be sure to place your robot on blocks before sending it
340 | your first Twist command.
341 |
342 | Launching the ros\_arduino\_python Node
343 | ---------------------------------------
344 | Take a look at the launch file arduino.launch in the
345 | ros\_arduino\_python/launch directory. As you can see, it points to a
346 | config file called my\_arduino\_params.yaml. If you named your config
347 | file something different, change the name in the launch file.
348 |
349 | With your Arduino connected and running the MegaRobogaiaPololu sketch,
350 | launch the ros\_arduino\_python node with your parameters:
351 |
352 | $ roslaunch ros_arduino_python arduino.launch
353 |
354 | You should see something like the following output:
355 |
356 |
357 | process[arduino-1]: started with pid [6098]
358 | Connecting to Arduino on port /dev/ttyUSB0 ...
359 | Connected at 57600
360 | Arduino is ready.
361 | [INFO] [WallTime: 1355498525.954491] Connected to Arduino on port /dev/ttyUSB0 at 57600 baud
362 | [INFO] [WallTime: 1355498525.966825] motor_current_right {'rate': 5, 'type': 'PololuMotorCurrent', 'pin': 1}
363 | [INFO]
364 | etc
365 |
366 |
367 | If you have any Ping sonar sensors on your robot and you defined them
368 | in your config file, they should start flashing to indicate you have
369 | made the connection.
370 |
371 | Viewing Sensor Data
372 | -------------------
373 | To see the aggregated sensor data, echo the sensor state topic:
374 |
375 | $ rostopic echo /arduino/sensor_state
376 |
377 | To see the data on any particular sensor, echo its topic name:
378 |
379 | $ rostopic echo /arduino/sensor/sensor_name
380 |
381 | For example, if you have a sensor called ir\_front\_center, you can see
382 | its data using:
383 |
384 | $ rostopic echo /arduino/sensor/ir_front_center
385 |
386 | You can also graph the range data using rxplot:
387 |
388 | $ rxplot -p 60 /arduino/sensor/ir_front_center/range
389 |
390 |
391 | Sending Twist Commands and Viewing Odometry Data
392 | ------------------------------------------------
393 |
394 | Place your robot on blocks, then try publishing a Twist command:
395 |
396 | $ rostopic pub -1 /cmd_vel geometry_msgs/Twist '{ angular: {z: 0.5} }'
397 |
398 | The wheels should turn in a direction consistent with a
399 | counter-clockwise rotation (right wheel forward, left wheel backward).
400 | If they turn in the opposite direction, set the motors_reversed
401 | parameter in your config file to the opposite of its current setting,
402 | then kill and restart the arduino.launch file.
403 |
404 | Stop the robot with the command:
405 |
406 | $ rostopic pub -1 /cmd_vel geometry_msgs/Twist '{}'
407 |
408 | To view odometry data:
409 |
410 | $ rostopic echo /odom
411 |
412 | or
413 |
414 | $ rxplot -p 60 /odom/pose/pose/position/x:y, /odom/twist/twist/linear/x, /odom/twist/twist/angular/z
415 |
416 | ROS Services
417 | ------------
418 | The ros\_arduino\_python package also defines a few ROS services as follows:
419 |
420 | **digital\_set\_direction** - set the direction of a digital pin
421 |
422 | $ rosservice call /arduino/digital_set_direction pin direction
423 |
424 | where pin is the pin number and direction is 0 for input and 1 for output.
425 |
426 | **digital\_write** - send a LOW (0) or HIGH (1) signal to a digital pin
427 |
428 | $ rosservice call /arduino/digital_write pin value
429 |
430 | where pin is the pin number and value is 0 for LOW and 1 for HIGH.
431 |
432 | **servo\_write** - set the position of a servo
433 |
434 | $ rosservice call /arduino/servo_write id pos
435 |
436 | where id is the index of the servo as defined in the Arduino sketch (servos.h) and pos is the position in radians (0 - 3.14).
437 |
438 | **servo\_read** - read the position of a servo
439 |
440 | $ rosservice call /arduino/servo_read id
441 |
442 | where id is the index of the servo as defined in the Arduino sketch (servos.h)
443 |
444 | Using the on-board wheel encoder counters (Arduino Uno only)
445 | ------------------------------------------------------------
446 | The firmware supports on-board wheel encoder counters for Arduino Uno.
447 | This allows connecting wheel encoders directly to the Arduino board, without the need for any additional wheel encoder counter equipment (such as a RoboGaia encoder shield).
448 |
449 | For speed, the code is directly addressing specific Atmega328p ports and interrupts, making this implementation Atmega328p (Arduino Uno) dependent. (It should be easy to adapt for other boards/AVR chips though.)
450 |
451 | To use the on-board wheel encoder counters, connect your wheel encoders to Arduino Uno as follows:
452 |
453 | Left wheel encoder A output -- Arduino UNO pin 2
454 | Left wheel encoder B output -- Arduino UNO pin 3
455 |
456 | Right wheel encoder A output -- Arduino UNO pin A4
457 | Right wheel encoder B output -- Arduino UNO pin A5
458 |
459 | Make the following changes in the ROSArduinoBridge sketch to disable the RoboGaia encoder shield, and enable the on-board one:
460 |
461 | /* The RoboGaia encoder shield */
462 | //#define ROBOGAIA
463 | /* Encoders directly attached to Arduino board */
464 | #define ARDUINO_ENC_COUNTER
465 |
466 | Compile the changes and upload to your controller.
467 |
468 | Using L298 Motor driver
469 | -----------------------
470 | the wiring between the L298 motor driver and arduino board is defined in motor_driver.h in the firmware as follow:
471 |
472 | #define RIGHT_MOTOR_BACKWARD 5
473 | #define LEFT_MOTOR_BACKWARD 6
474 | #define RIGHT_MOTOR_FORWARD 9
475 | #define LEFT_MOTOR_FORWARD 10
476 | #define RIGHT_MOTOR_ENABLE 12
477 | #define LEFT_MOTOR_ENABLE 13
478 |
479 | wire them this way or change them if you want, and make sure that the L298 motor driver is defined then compile and upload the firmware.
480 |
481 | NOTES
482 | -----
483 | If you do not have the hardware required to run the base controller,
484 | follow the instructions below so that you can still use your
485 | Arduino-compatible controller to read sensors and control PWM servos.
486 |
487 | First, you need to edit the ROSArduinoBridge sketch. At the top of
488 | the file comment out the line:
489 |
490 |
491 | #define USE_BASE
492 |
493 |
494 | so that it looks like this:
495 |
496 |
497 | //#define USE_BASE
498 |
499 |
500 | **NOTE:** If you are using a version of the Arduino IDE previous to 1.6.6, you also need to comment out the line that looks like this in the file encoder_driver.ino:
501 |
502 | #include "MegaEncoderCounter.h"
503 |
504 | so it looks like this:
505 |
506 | //#include "MegaEncoderCounter.h"
507 |
508 | Compile the changes and upload to your controller.
509 |
510 | Next, edit your my\_arduino_params.yaml file and make sure the
511 | use\_base\_controller parameter is set to False. That's all there is to it.
512 |
--------------------------------------------------------------------------------
/ros_arduino_bridge/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.8.3)
2 | project(ros_arduino_bridge)
3 | find_package(catkin REQUIRED)
4 | catkin_metapackage()
5 |
--------------------------------------------------------------------------------
/ros_arduino_bridge/package.xml:
--------------------------------------------------------------------------------
1 |
2 | ros_arduino_bridge
3 | 0.2.0
4 |
5 | Metapackage for ros_arduino_bridge.
6 |
7 | Patrick Goebel
8 | Patrick Goebel
9 | BSD
10 | http://ros.org/wiki/ros_arduino_bridge
11 |
12 | catkin
13 |
14 | ros_arduino_firmware
15 | ros_arduino_msgs
16 | ros_arduino_python
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.8.3)
2 | project(ros_arduino_firmware)
3 |
4 | find_package(catkin REQUIRED)
5 | catkin_package(DEPENDS)
6 |
7 | install(DIRECTORY src
8 | DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
9 | )
10 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/package.xml:
--------------------------------------------------------------------------------
1 |
2 | ros_arduino_firmware
3 | 0.2.0
4 |
5 | ROS Arduino Firmware.
6 |
7 | Patrick Goebel
8 | Patrick Goebel
9 | BSD
10 | http://ros.org/wiki/ros_arduino_firmware
11 |
12 | catkin
13 |
14 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/MegaRobogaiaPololu/MegaRobogaiaPololu.ino:
--------------------------------------------------------------------------------
1 | /*********************************************************************
2 | * ROSArduinoBridge
3 |
4 | A set of simple serial commands to control a differential drive
5 | robot and receive back sensor and odometry data. Default
6 | configuration assumes use of an Arduino Mega + Pololu motor
7 | controller shield + Robogaia Mega Encoder shield. Edit the
8 | readEncoder() and setMotorSpeed() wrapper functions if using
9 | different motor controller or encoder method.
10 |
11 | Created for the Pi Robot Project: http://www.pirobot.org
12 |
13 | Inspired and modeled after the ArbotiX driver by Michael Ferguson
14 |
15 | Software License Agreement (BSD License)
16 |
17 | Copyright (c) 2012, Patrick Goebel.
18 | All rights reserved.
19 |
20 | Redistribution and use in source and binary forms, with or without
21 | modification, are permitted provided that the following conditions
22 | are met:
23 |
24 | * Redistributions of source code must retain the above copyright
25 | notice, this list of conditions and the following disclaimer.
26 | * Redistributions in binary form must reproduce the above
27 | copyright notice, this list of conditions and the following
28 | disclaimer in the documentation and/or other materials provided
29 | with the distribution.
30 |
31 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
36 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
37 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
41 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 | * POSSIBILITY OF SUCH DAMAGE.
43 | *********************************************************************/
44 |
45 | #define USE_BASE // Enable the base controller code
46 | //#undef USE_BASE // Disable the base controller code
47 |
48 | //#define USE_SERVOS // Enable use of PWM servos as defined in servos.h
49 | #undef USE_SERVOS // Disable use of PWM servos
50 |
51 | /* Serial port baud rate */
52 | #define BAUDRATE 57600
53 |
54 | /* Maximum PWM signal */
55 | #define MAX_PWM 255
56 |
57 | #if defined(ARDUINO) && ARDUINO >= 100
58 | #include "Arduino.h"
59 | #else
60 | #include "WProgram.h"
61 | #endif
62 |
63 | /* Include definition of serial commands */
64 | #include "commands.h"
65 |
66 | /* Sensor functions */
67 | #include "sensors.h"
68 |
69 | /* Include servo support if required */
70 | #ifdef USE_SERVOS
71 | #include
72 | #include "servos.h"
73 | #endif
74 |
75 | #ifdef USE_BASE
76 | /* The Pololu motor driver shield */
77 | #include "DualVNH5019MotorShield.h"
78 |
79 | /* The Robogaia Mega Encoder shield */
80 | #include "MegaEncoderCounter.h"
81 |
82 | /* Create the motor driver object */
83 | DualVNH5019MotorShield drive;
84 |
85 | /* Create the encoder shield object */
86 | MegaEncoderCounter encoders(4); // Initializes the Mega Encoder Counter in the 4X Count mode
87 |
88 | /* PID parameters and functions */
89 | #include "diff_controller.h"
90 |
91 | /* Run the PID loop at 30 times per second */
92 | #define PID_RATE 30 // Hz
93 |
94 | /* Convert the rate into an interval */
95 | const int PID_INTERVAL = 1000 / PID_RATE;
96 |
97 | /* Track the next time we make a PID calculation */
98 | unsigned long nextPID = PID_INTERVAL;
99 |
100 | /* Stop the robot if it hasn't received a movement command
101 | in this number of milliseconds */
102 | #define AUTO_STOP_INTERVAL 2000
103 | long lastMotorCommand = AUTO_STOP_INTERVAL;
104 |
105 | /* Wrap the encoder reading function */
106 | long readEncoder(int i) {
107 | if (i == LEFT) return encoders.YAxisGetCount();
108 | else return encoders.XAxisGetCount();
109 | }
110 |
111 | /* Wrap the encoder reset function */
112 | void resetEncoder(int i) {
113 | if (i == LEFT) return encoders.YAxisReset();
114 | else return encoders.XAxisReset();
115 | }
116 |
117 | /* Wrap the encoder reset function */
118 | void resetEncoders() {
119 | resetEncoder(LEFT);
120 | resetEncoder(RIGHT);
121 | }
122 |
123 | /* Wrap the motor driver initialization */
124 | void initMotorController() {
125 | drive.init();
126 | }
127 |
128 | /* Wrap the drive motor set speed function */
129 | void setMotorSpeed(int i, int spd) {
130 | if (i == LEFT) drive.setM1Speed(spd);
131 | else drive.setM2Speed(spd);
132 | }
133 |
134 | // A convenience function for setting both motor speeds
135 | void setMotorSpeeds(int leftSpeed, int rightSpeed) {
136 | setMotorSpeed(LEFT, leftSpeed);
137 | setMotorSpeed(RIGHT, rightSpeed);
138 | }
139 | #endif
140 |
141 | /* Variable initialization */
142 |
143 | // A pair of varibles to help parse serial commands (thanks Fergs)
144 | int arg = 0;
145 | int index = 0;
146 |
147 | // Variable to hold an input character
148 | char chr;
149 |
150 | // Variable to hold the current single-character command
151 | char cmd;
152 |
153 | // Character arrays to hold the first and second arguments
154 | char argv1[16];
155 | char argv2[16];
156 |
157 | // The arguments converted to integers
158 | long arg1;
159 | long arg2;
160 |
161 | /* Clear the current command parameters */
162 | void resetCommand() {
163 | cmd = NULL;
164 | memset(argv1, 0, sizeof(argv1));
165 | memset(argv2, 0, sizeof(argv2));
166 | arg1 = 0;
167 | arg2 = 0;
168 | arg = 0;
169 | index = 0;
170 | }
171 |
172 | /* Run a command. Commands are defined in commands.h */
173 | int runCommand() {
174 | int i = 0;
175 | char *p = argv1;
176 | char *str;
177 | int pid_args[4];
178 | arg1 = atoi(argv1);
179 | arg2 = atoi(argv2);
180 |
181 | switch(cmd) {
182 | case GET_BAUDRATE:
183 | Serial.println(BAUDRATE);
184 | break;
185 | case ANALOG_READ:
186 | Serial.println(analogRead(arg1));
187 | break;
188 | case DIGITAL_READ:
189 | Serial.println(digitalRead(arg1));
190 | break;
191 | case ANALOG_WRITE:
192 | analogWrite(arg1, arg2);
193 | Serial.println("OK");
194 | break;
195 | case DIGITAL_WRITE:
196 | if (arg2 == 0) digitalWrite(arg1, LOW);
197 | else if (arg2 == 1) digitalWrite(arg1, HIGH);
198 | Serial.println("OK");
199 | break;
200 | case PIN_MODE:
201 | if (arg2 == 0) pinMode(arg1, INPUT);
202 | else if (arg2 == 1) pinMode(arg1, OUTPUT);
203 | Serial.println("OK");
204 | break;
205 | case PING:
206 | Serial.println(Ping(arg1));
207 | break;
208 | #ifdef USE_SERVOS
209 | case SERVO_WRITE:
210 | servos[arg1].write(arg2);
211 | Serial.println("OK");
212 | break;
213 | case SERVO_READ:
214 | Serial.println(servos[arg1].read());
215 | break;
216 | #endif
217 |
218 | #ifdef USE_BASE
219 | case READ_ENCODERS:
220 | Serial.print(readEncoder(LEFT));
221 | Serial.print(" ");
222 | Serial.println(readEncoder(RIGHT));
223 | break;
224 | case RESET_ENCODERS:
225 | resetEncoders();
226 | Serial.println("OK");
227 | break;
228 | case MOTOR_SPEEDS:
229 | /* Reset the auto stop timer */
230 | lastMotorCommand = millis();
231 | if (arg1 == 0 && arg2 == 0) {
232 | setMotorSpeeds(0, 0);
233 | moving = 0;
234 | }
235 | else moving = 1;
236 | leftPID.TargetTicksPerFrame = arg1;
237 | rightPID.TargetTicksPerFrame = arg2;
238 | Serial.println("OK");
239 | break;
240 | case UPDATE_PID:
241 | while ((str = strtok_r(p, ":", &p)) != '\0') {
242 | pid_args[i] = atoi(str);
243 | i++;
244 | }
245 | Kp = pid_args[0];
246 | Kd = pid_args[1];
247 | Ki = pid_args[2];
248 | Ko = pid_args[3];
249 | Serial.println("OK");
250 | break;
251 | #endif
252 | default:
253 | Serial.println("Invalid Command");
254 | break;
255 | }
256 | }
257 |
258 | /* Setup function--runs once at startup. */
259 | void setup() {
260 | Serial.begin(BAUDRATE);
261 |
262 | // Initialize the motor controller if used */
263 | #ifdef USE_BASE
264 | initMotorController();
265 | #endif
266 |
267 | /* Attach servos if used */
268 | #ifdef USE_SERVOS
269 | int i;
270 | for (i = 0; i < N_SERVOS; i++) {
271 | servos[i].attach(servoPins[i]);
272 | }
273 | #endif
274 | }
275 |
276 | /* Enter the main loop. Read and parse input from the serial port
277 | and run any valid commands. Run a PID calculation at the target
278 | interval and check for auto-stop conditions.
279 | */
280 | void loop() {
281 | while (Serial.available() > 0) {
282 |
283 | // Read the next character
284 | chr = Serial.read();
285 |
286 | // Terminate a command with a CR
287 | if (chr == 13) {
288 | if (arg == 1) argv1[index] = NULL;
289 | else if (arg == 2) argv2[index] = NULL;
290 | runCommand();
291 | resetCommand();
292 | }
293 | // Use spaces to delimit parts of the command
294 | else if (chr == ' ') {
295 | // Step through the arguments
296 | if (arg == 0) arg = 1;
297 | else if (arg == 1) {
298 | argv1[index] = NULL;
299 | arg = 2;
300 | index = 0;
301 | }
302 | continue;
303 | }
304 | else {
305 | if (arg == 0) {
306 | // The first arg is the single-letter command
307 | cmd = chr;
308 | }
309 | else if (arg == 1) {
310 | // Subsequent arguments can be more than one character
311 | argv1[index] = chr;
312 | index++;
313 | }
314 | else if (arg == 2) {
315 | argv2[index] = chr;
316 | index++;
317 | }
318 | }
319 | }
320 |
321 | // If we are using base control, run a PID calculation at the appropriate intervals
322 | #ifdef USE_BASE
323 | if (millis() > nextPID) {
324 | updatePID();
325 | nextPID += PID_INTERVAL;
326 | }
327 |
328 | // Check to see if we have exceeded the auto-stop interval
329 | if ((millis() - lastMotorCommand) > AUTO_STOP_INTERVAL) {;
330 | setMotorSpeeds(0, 0);
331 | moving = 0;
332 | }
333 |
334 | #endif
335 | }
336 |
337 |
338 |
339 |
340 |
341 |
342 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/MegaRobogaiaPololu/commands.h:
--------------------------------------------------------------------------------
1 | /* Define single-letter commands that will be sent by the PC over the
2 | serial link.
3 | */
4 |
5 | #define ANALOG_READ 'a'
6 | #define GET_BAUDRATE 'b'
7 | #define PIN_MODE 'c'
8 | #define DIGITAL_READ 'd'
9 | #define READ_ENCODERS 'e'
10 | #define MOTOR_SPEEDS 'm'
11 | #define PING 'p'
12 | #define RESET_ENCODERS 'r'
13 | #define SERVO_WRITE 's'
14 | #define SERVO_READ 't'
15 | #define UPDATE_PID 'u'
16 | #define DIGITAL_WRITE 'w'
17 | #define ANALOG_WRITE 'x'
18 | #define LEFT 0
19 | #define RIGHT 1
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/MegaRobogaiaPololu/diff_controller.h:
--------------------------------------------------------------------------------
1 | /* Functions and type-defs for PID control.
2 |
3 | Taken mostly from Mike Ferguson's ArbotiX code which lives at:
4 |
5 | http://vanadium-ros-pkg.googlecode.com/svn/trunk/arbotix/
6 | */
7 |
8 | /* PID setpoint info For a Motor */
9 | typedef struct {
10 | double TargetTicksPerFrame; // target speed in ticks per frame
11 | long Encoder; // encoder count
12 | long PrevEnc; // last encoder count
13 | int PrevErr; // last error
14 | int Ierror; // integrated error
15 | int output; // last motor setting
16 | }
17 | SetPointInfo;
18 |
19 | SetPointInfo leftPID, rightPID;
20 |
21 | /* PID Parameters */
22 | int Kp = 20;
23 | int Kd = 12;
24 | int Ki = 0;
25 | int Ko = 50;
26 |
27 | unsigned char moving = 0; // is the base in motion?
28 |
29 | /* PID routine to compute the next motor commands */
30 | void doPID(SetPointInfo * p) {
31 | long Perror;
32 | long output;
33 |
34 | Perror = p->TargetTicksPerFrame - (p->Encoder - p->PrevEnc);
35 |
36 | // Derivative error is the delta Perror
37 | output = (Kp * Perror + Kd * (Perror - p->PrevErr) + Ki * p->Ierror) / Ko;
38 | p->PrevErr = Perror;
39 | p->PrevEnc = p->Encoder;
40 |
41 | output += p->output;
42 | // Accumulate Integral error *or* Limit output.
43 | // Stop accumulating when output saturates
44 | if (output >= MAX_PWM)
45 | output = MAX_PWM;
46 | else if (output <= -MAX_PWM)
47 | output = -MAX_PWM;
48 | else
49 | p->Ierror += Perror;
50 |
51 | p->output = output;
52 | }
53 |
54 | /* Read the encoder values and call the PID routine */
55 | void updatePID() {
56 | /* Read the encoders */
57 | leftPID.Encoder = readEncoder(0);
58 | rightPID.Encoder = readEncoder(1);
59 |
60 | /* If we're not moving there is nothing more to do */
61 | if (!moving)
62 | return;
63 |
64 | /* Compute PID update for each motor */
65 | doPID(&rightPID);
66 | doPID(&leftPID);
67 |
68 | /* Set the motor speeds accordingly */
69 | setMotorSpeeds(leftPID.output, rightPID.output);
70 | }
71 |
72 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/MegaRobogaiaPololu/sensors.h:
--------------------------------------------------------------------------------
1 | /* Functions for various sensor types */
2 |
3 | float microsecondsToCm(long microseconds)
4 | {
5 | // The speed of sound is 340 m/s or 29 microseconds per cm.
6 | // The ping travels out and back, so to find the distance of the
7 | // object we take half of the distance travelled.
8 | return microseconds / 29 / 2;
9 | }
10 |
11 | long Ping(int pin) {
12 | long duration, range;
13 |
14 | // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
15 | // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
16 | pinMode(pin, OUTPUT);
17 | digitalWrite(pin, LOW);
18 | delayMicroseconds(2);
19 | digitalWrite(pin, HIGH);
20 | delayMicroseconds(5);
21 | digitalWrite(pin, LOW);
22 |
23 | // The same pin is used to read the signal from the PING))): a HIGH
24 | // pulse whose duration is the time (in microseconds) from the sending
25 | // of the ping to the reception of its echo off of an object.
26 | pinMode(pin, INPUT);
27 | duration = pulseIn(pin, HIGH);
28 |
29 | // convert the time into meters
30 | range = microsecondsToCm(duration);
31 |
32 | return(range);
33 | }
34 |
35 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/MegaRobogaiaPololu/servos.h:
--------------------------------------------------------------------------------
1 | /* Define the attachment of any servos here.
2 | The example shows two servos attached on pins 3 and 5.
3 | */
4 |
5 | #define N_SERVOS 2
6 |
7 | Servo servos [N_SERVOS];
8 | byte servoPins [N_SERVOS] = {3, 5};
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/ROSArduinoBridge.ino:
--------------------------------------------------------------------------------
1 | /*********************************************************************
2 | * ROSArduinoBridge
3 |
4 | A set of simple serial commands to control a differential drive
5 | robot and receive back sensor and odometry data. Default
6 | configuration assumes use of an Arduino Mega + Pololu motor
7 | controller shield + Robogaia Mega Encoder shield. Edit the
8 | readEncoder() and setMotorSpeed() wrapper functions if using
9 | different motor controller or encoder method.
10 |
11 | Created for the Pi Robot Project: http://www.pirobot.org
12 | and the Home Brew Robotics Club (HBRC): http://hbrobotics.org
13 |
14 | Authors: Patrick Goebel, James Nugen
15 |
16 | Inspired and modeled after the ArbotiX driver by Michael Ferguson
17 |
18 | Software License Agreement (BSD License)
19 |
20 | Copyright (c) 2012, Patrick Goebel.
21 | All rights reserved.
22 |
23 | Redistribution and use in source and binary forms, with or without
24 | modification, are permitted provided that the following conditions
25 | are met:
26 |
27 | * Redistributions of source code must retain the above copyright
28 | notice, this list of conditions and the following disclaimer.
29 | * Redistributions in binary form must reproduce the above
30 | copyright notice, this list of conditions and the following
31 | disclaimer in the documentation and/or other materials provided
32 | with the distribution.
33 |
34 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
38 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
39 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
40 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
44 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 | * POSSIBILITY OF SUCH DAMAGE.
46 | *********************************************************************/
47 |
48 | //#define USE_BASE // Enable the base controller code
49 | #undef USE_BASE // Disable the base controller code
50 |
51 | /* Define the motor controller and encoder library you are using */
52 | #ifdef USE_BASE
53 | /* The Pololu VNH5019 dual motor driver shield */
54 | #define POLOLU_VNH5019
55 |
56 | /* The Pololu MC33926 dual motor driver shield */
57 | //#define POLOLU_MC33926
58 |
59 | /* The RoboGaia encoder shield */
60 | #define ROBOGAIA
61 |
62 | /* Encoders directly attached to Arduino board */
63 | //#define ARDUINO_ENC_COUNTER
64 |
65 | /* L298 Motor driver*/
66 | //#define L298_MOTOR_DRIVER
67 | #endif
68 |
69 | #define USE_SERVOS // Enable use of PWM servos as defined in servos.h
70 | //#undef USE_SERVOS // Disable use of PWM servos
71 |
72 | /* Serial port baud rate */
73 | #define BAUDRATE 57600
74 |
75 | /* Maximum PWM signal */
76 | #define MAX_PWM 255
77 |
78 | #if defined(ARDUINO) && ARDUINO >= 100
79 | #include "Arduino.h"
80 | #else
81 | #include "WProgram.h"
82 | #endif
83 |
84 | /* Include definition of serial commands */
85 | #include "commands.h"
86 |
87 | /* Sensor functions */
88 | #include "sensors.h"
89 |
90 | /* Include servo support if required */
91 | #ifdef USE_SERVOS
92 | #include
93 | #include "servos.h"
94 | #endif
95 |
96 | #ifdef USE_BASE
97 | /* Motor driver function definitions */
98 | #include "motor_driver.h"
99 |
100 | /* Encoder driver function definitions */
101 | #include "encoder_driver.h"
102 |
103 | /* PID parameters and functions */
104 | #include "diff_controller.h"
105 |
106 | /* Run the PID loop at 30 times per second */
107 | #define PID_RATE 30 // Hz
108 |
109 | /* Convert the rate into an interval */
110 | const int PID_INTERVAL = 1000 / PID_RATE;
111 |
112 | /* Track the next time we make a PID calculation */
113 | unsigned long nextPID = PID_INTERVAL;
114 |
115 | /* Stop the robot if it hasn't received a movement command
116 | in this number of milliseconds */
117 | #define AUTO_STOP_INTERVAL 2000
118 | long lastMotorCommand = AUTO_STOP_INTERVAL;
119 | #endif
120 |
121 | /* Variable initialization */
122 |
123 | // A pair of varibles to help parse serial commands (thanks Fergs)
124 | int arg = 0;
125 | int index = 0;
126 |
127 | // Variable to hold an input character
128 | char chr;
129 |
130 | // Variable to hold the current single-character command
131 | char cmd;
132 |
133 | // Character arrays to hold the first and second arguments
134 | char argv1[16];
135 | char argv2[16];
136 |
137 | // The arguments converted to integers
138 | long arg1;
139 | long arg2;
140 |
141 | /* Clear the current command parameters */
142 | void resetCommand() {
143 | cmd = NULL;
144 | memset(argv1, 0, sizeof(argv1));
145 | memset(argv2, 0, sizeof(argv2));
146 | arg1 = 0;
147 | arg2 = 0;
148 | arg = 0;
149 | index = 0;
150 | }
151 |
152 | /* Run a command. Commands are defined in commands.h */
153 | int runCommand() {
154 | int i = 0;
155 | char *p = argv1;
156 | char *str;
157 | int pid_args[4];
158 | arg1 = atoi(argv1);
159 | arg2 = atoi(argv2);
160 |
161 | switch(cmd) {
162 | case GET_BAUDRATE:
163 | Serial.println(BAUDRATE);
164 | break;
165 | case ANALOG_READ:
166 | Serial.println(analogRead(arg1));
167 | break;
168 | case DIGITAL_READ:
169 | Serial.println(digitalRead(arg1));
170 | break;
171 | case ANALOG_WRITE:
172 | analogWrite(arg1, arg2);
173 | Serial.println("OK");
174 | break;
175 | case DIGITAL_WRITE:
176 | if (arg2 == 0) digitalWrite(arg1, LOW);
177 | else if (arg2 == 1) digitalWrite(arg1, HIGH);
178 | Serial.println("OK");
179 | break;
180 | case PIN_MODE:
181 | if (arg2 == 0) pinMode(arg1, INPUT);
182 | else if (arg2 == 1) pinMode(arg1, OUTPUT);
183 | Serial.println("OK");
184 | break;
185 | case PING:
186 | Serial.println(Ping(arg1));
187 | break;
188 | #ifdef USE_SERVOS
189 | case SERVO_WRITE:
190 | servos[arg1].setTargetPosition(arg2);
191 | Serial.println("OK");
192 | break;
193 | case SERVO_READ:
194 | Serial.println(servos[arg1].getServo().read());
195 | break;
196 | #endif
197 |
198 | #ifdef USE_BASE
199 | case READ_ENCODERS:
200 | Serial.print(readEncoder(LEFT));
201 | Serial.print(" ");
202 | Serial.println(readEncoder(RIGHT));
203 | break;
204 | case RESET_ENCODERS:
205 | resetEncoders();
206 | resetPID();
207 | Serial.println("OK");
208 | break;
209 | case MOTOR_SPEEDS:
210 | /* Reset the auto stop timer */
211 | lastMotorCommand = millis();
212 | if (arg1 == 0 && arg2 == 0) {
213 | setMotorSpeeds(0, 0);
214 | resetPID();
215 | moving = 0;
216 | }
217 | else moving = 1;
218 | leftPID.TargetTicksPerFrame = arg1;
219 | rightPID.TargetTicksPerFrame = arg2;
220 | Serial.println("OK");
221 | break;
222 | case UPDATE_PID:
223 | while ((str = strtok_r(p, ":", &p)) != '\0') {
224 | pid_args[i] = atoi(str);
225 | i++;
226 | }
227 | Kp = pid_args[0];
228 | Kd = pid_args[1];
229 | Ki = pid_args[2];
230 | Ko = pid_args[3];
231 | Serial.println("OK");
232 | break;
233 | #endif
234 | default:
235 | Serial.println("Invalid Command");
236 | break;
237 | }
238 | }
239 |
240 | /* Setup function--runs once at startup. */
241 | void setup() {
242 | Serial.begin(BAUDRATE);
243 |
244 | // Initialize the motor controller if used */
245 | #ifdef USE_BASE
246 | #ifdef ARDUINO_ENC_COUNTER
247 | //set as inputs
248 | DDRD &= ~(1< 0) {
289 |
290 | // Read the next character
291 | chr = Serial.read();
292 |
293 | // Terminate a command with a CR
294 | if (chr == 13) {
295 | if (arg == 1) argv1[index] = NULL;
296 | else if (arg == 2) argv2[index] = NULL;
297 | runCommand();
298 | resetCommand();
299 | }
300 | // Use spaces to delimit parts of the command
301 | else if (chr == ' ') {
302 | // Step through the arguments
303 | if (arg == 0) arg = 1;
304 | else if (arg == 1) {
305 | argv1[index] = NULL;
306 | arg = 2;
307 | index = 0;
308 | }
309 | continue;
310 | }
311 | else {
312 | if (arg == 0) {
313 | // The first arg is the single-letter command
314 | cmd = chr;
315 | }
316 | else if (arg == 1) {
317 | // Subsequent arguments can be more than one character
318 | argv1[index] = chr;
319 | index++;
320 | }
321 | else if (arg == 2) {
322 | argv2[index] = chr;
323 | index++;
324 | }
325 | }
326 | }
327 |
328 | // If we are using base control, run a PID calculation at the appropriate intervals
329 | #ifdef USE_BASE
330 | if (millis() > nextPID) {
331 | updatePID();
332 | nextPID += PID_INTERVAL;
333 | }
334 |
335 | // Check to see if we have exceeded the auto-stop interval
336 | if ((millis() - lastMotorCommand) > AUTO_STOP_INTERVAL) {;
337 | setMotorSpeeds(0, 0);
338 | moving = 0;
339 | }
340 | #endif
341 |
342 | // Sweep servos
343 | #ifdef USE_SERVOS
344 | int i;
345 | for (i = 0; i < N_SERVOS; i++) {
346 | servos[i].doSweep();
347 | }
348 | #endif
349 | }
350 |
351 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/commands.h:
--------------------------------------------------------------------------------
1 | /* Define single-letter commands that will be sent by the PC over the
2 | serial link.
3 | */
4 |
5 | #ifndef COMMANDS_H
6 | #define COMMANDS_H
7 |
8 | #define ANALOG_READ 'a'
9 | #define GET_BAUDRATE 'b'
10 | #define PIN_MODE 'c'
11 | #define DIGITAL_READ 'd'
12 | #define READ_ENCODERS 'e'
13 | #define MOTOR_SPEEDS 'm'
14 | #define PING 'p'
15 | #define RESET_ENCODERS 'r'
16 | #define SERVO_WRITE 's'
17 | #define SERVO_READ 't'
18 | #define UPDATE_PID 'u'
19 | #define DIGITAL_WRITE 'w'
20 | #define ANALOG_WRITE 'x'
21 | #define LEFT 0
22 | #define RIGHT 1
23 |
24 | #endif
25 |
26 |
27 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/diff_controller.h:
--------------------------------------------------------------------------------
1 | /* Functions and type-defs for PID control.
2 |
3 | Taken mostly from Mike Ferguson's ArbotiX code which lives at:
4 |
5 | http://vanadium-ros-pkg.googlecode.com/svn/trunk/arbotix/
6 | */
7 |
8 | /* PID setpoint info For a Motor */
9 | typedef struct {
10 | double TargetTicksPerFrame; // target speed in ticks per frame
11 | long Encoder; // encoder count
12 | long PrevEnc; // last encoder count
13 |
14 | /*
15 | * Using previous input (PrevInput) instead of PrevError to avoid derivative kick,
16 | * see http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-derivative-kick/
17 | */
18 | int PrevInput; // last input
19 | //int PrevErr; // last error
20 |
21 | /*
22 | * Using integrated term (ITerm) instead of integrated error (Ierror),
23 | * to allow tuning changes,
24 | * see http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-tuning-changes/
25 | */
26 | //int Ierror;
27 | int ITerm; //integrated term
28 |
29 | long output; // last motor setting
30 | }
31 | SetPointInfo;
32 |
33 | SetPointInfo leftPID, rightPID;
34 |
35 | /* PID Parameters */
36 | int Kp = 20;
37 | int Kd = 12;
38 | int Ki = 0;
39 | int Ko = 50;
40 |
41 | unsigned char moving = 0; // is the base in motion?
42 |
43 | /*
44 | * Initialize PID variables to zero to prevent startup spikes
45 | * when turning PID on to start moving
46 | * In particular, assign both Encoder and PrevEnc the current encoder value
47 | * See http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-initialization/
48 | * Note that the assumption here is that PID is only turned on
49 | * when going from stop to moving, that's why we can init everything on zero.
50 | */
51 | void resetPID(){
52 | leftPID.TargetTicksPerFrame = 0.0;
53 | leftPID.Encoder = readEncoder(LEFT);
54 | leftPID.PrevEnc = leftPID.Encoder;
55 | leftPID.output = 0;
56 | leftPID.PrevInput = 0;
57 | leftPID.ITerm = 0;
58 |
59 | rightPID.TargetTicksPerFrame = 0.0;
60 | rightPID.Encoder = readEncoder(RIGHT);
61 | rightPID.PrevEnc = rightPID.Encoder;
62 | rightPID.output = 0;
63 | rightPID.PrevInput = 0;
64 | rightPID.ITerm = 0;
65 | }
66 |
67 | /* PID routine to compute the next motor commands */
68 | void doPID(SetPointInfo * p) {
69 | long Perror;
70 | long output;
71 | int input;
72 |
73 | //Perror = p->TargetTicksPerFrame - (p->Encoder - p->PrevEnc);
74 | input = p->Encoder - p->PrevEnc;
75 | Perror = p->TargetTicksPerFrame - input;
76 |
77 |
78 | /*
79 | * Avoid derivative kick and allow tuning changes,
80 | * see http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-derivative-kick/
81 | * see http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-tuning-changes/
82 | */
83 | //output = (Kp * Perror + Kd * (Perror - p->PrevErr) + Ki * p->Ierror) / Ko;
84 | // p->PrevErr = Perror;
85 | output = (Kp * Perror - Kd * (input - p->PrevInput) + p->ITerm) / Ko;
86 | p->PrevEnc = p->Encoder;
87 |
88 | output += p->output;
89 | // Accumulate Integral error *or* Limit output.
90 | // Stop accumulating when output saturates
91 | if (output >= MAX_PWM)
92 | output = MAX_PWM;
93 | else if (output <= -MAX_PWM)
94 | output = -MAX_PWM;
95 | else
96 | /*
97 | * allow turning changes, see http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-tuning-changes/
98 | */
99 | p->ITerm += Ki * Perror;
100 |
101 | p->output = output;
102 | p->PrevInput = input;
103 | }
104 |
105 | /* Read the encoder values and call the PID routine */
106 | void updatePID() {
107 | /* Read the encoders */
108 | leftPID.Encoder = readEncoder(LEFT);
109 | rightPID.Encoder = readEncoder(RIGHT);
110 |
111 | /* If we're not moving there is nothing more to do */
112 | if (!moving){
113 | /*
114 | * Reset PIDs once, to prevent startup spikes,
115 | * see http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-initialization/
116 | * PrevInput is considered a good proxy to detect
117 | * whether reset has already happened
118 | */
119 | if (leftPID.PrevInput != 0 || rightPID.PrevInput != 0) resetPID();
120 | return;
121 | }
122 |
123 | /* Compute PID update for each motor */
124 | doPID(&rightPID);
125 | doPID(&leftPID);
126 |
127 | /* Set the motor speeds accordingly */
128 | setMotorSpeeds(leftPID.output, rightPID.output);
129 | }
130 |
131 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/encoder_driver.h:
--------------------------------------------------------------------------------
1 | /* *************************************************************
2 | Encoder driver function definitions - by James Nugen
3 | ************************************************************ */
4 |
5 |
6 | #ifdef ARDUINO_ENC_COUNTER
7 | //below can be changed, but should be PORTD pins;
8 | //otherwise additional changes in the code are required
9 | #define LEFT_ENC_PIN_A PD2 //pin 2
10 | #define LEFT_ENC_PIN_B PD3 //pin 3
11 |
12 | //below can be changed, but should be PORTC pins
13 | #define RIGHT_ENC_PIN_A PC4 //pin A4
14 | #define RIGHT_ENC_PIN_B PC5 //pin A5
15 | #endif
16 |
17 | long readEncoder(int i);
18 | void resetEncoder(int i);
19 | void resetEncoders();
20 |
21 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/encoder_driver.ino:
--------------------------------------------------------------------------------
1 | /* *************************************************************
2 | Encoder definitions
3 |
4 | Add an "#ifdef" block to this file to include support for
5 | a particular encoder board or library. Then add the appropriate
6 | #define near the top of the main ROSArduinoBridge.ino file.
7 |
8 | ************************************************************ */
9 |
10 | #ifdef USE_BASE
11 |
12 | #ifdef ROBOGAIA
13 | /* The Robogaia Mega Encoder shield */
14 | #include "MegaEncoderCounter.h"
15 |
16 | /* Create the encoder shield object */
17 | MegaEncoderCounter encoders = MegaEncoderCounter(4); // Initializes the Mega Encoder Counter in the 4X Count mode
18 |
19 | /* Wrap the encoder reading function */
20 | long readEncoder(int i) {
21 | if (i == LEFT) return encoders.YAxisGetCount();
22 | else return encoders.XAxisGetCount();
23 | }
24 |
25 | /* Wrap the encoder reset function */
26 | void resetEncoder(int i) {
27 | if (i == LEFT) return encoders.YAxisReset();
28 | else return encoders.XAxisReset();
29 | }
30 | #elif defined(ARDUINO_ENC_COUNTER)
31 | volatile long left_enc_pos = 0L;
32 | volatile long right_enc_pos = 0L;
33 | static const int8_t ENC_STATES [] = {0,1,-1,0,-1,0,0,1,1,0,0,-1,0,-1,1,0}; //encoder lookup table
34 |
35 | /* Interrupt routine for LEFT encoder, taking care of actual counting */
36 | ISR (PCINT2_vect){
37 | static uint8_t enc_last=0;
38 |
39 | enc_last <<=2; //shift previous state two places
40 | enc_last |= (PIND & (3 << 2)) >> 2; //read the current state into lowest 2 bits
41 |
42 | left_enc_pos += ENC_STATES[(enc_last & 0x0f)];
43 | }
44 |
45 | /* Interrupt routine for RIGHT encoder, taking care of actual counting */
46 | ISR (PCINT1_vect){
47 | static uint8_t enc_last=0;
48 |
49 | enc_last <<=2; //shift previous state two places
50 | enc_last |= (PINC & (3 << 4)) >> 4; //read the current state into lowest 2 bits
51 |
52 | right_enc_pos += ENC_STATES[(enc_last & 0x0f)];
53 | }
54 |
55 | /* Wrap the encoder reading function */
56 | long readEncoder(int i) {
57 | if (i == LEFT) return left_enc_pos;
58 | else return right_enc_pos;
59 | }
60 |
61 | /* Wrap the encoder reset function */
62 | void resetEncoder(int i) {
63 | if (i == LEFT){
64 | left_enc_pos=0L;
65 | return;
66 | } else {
67 | right_enc_pos=0L;
68 | return;
69 | }
70 | }
71 | #else
72 | #error A encoder driver must be selected!
73 | #endif
74 |
75 | /* Wrap the encoder reset function */
76 | void resetEncoders() {
77 | resetEncoder(LEFT);
78 | resetEncoder(RIGHT);
79 | }
80 |
81 | #endif
82 |
83 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/motor_driver.h:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 | Motor driver function definitions - by James Nugen
3 | *************************************************************/
4 |
5 | #ifdef L298_MOTOR_DRIVER
6 | #define RIGHT_MOTOR_BACKWARD 5
7 | #define LEFT_MOTOR_BACKWARD 6
8 | #define RIGHT_MOTOR_FORWARD 9
9 | #define LEFT_MOTOR_FORWARD 10
10 | #define RIGHT_MOTOR_ENABLE 12
11 | #define LEFT_MOTOR_ENABLE 13
12 | #endif
13 |
14 | void initMotorController();
15 | void setMotorSpeed(int i, int spd);
16 | void setMotorSpeeds(int leftSpeed, int rightSpeed);
17 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/motor_driver.ino:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 | Motor driver definitions
3 |
4 | Add a "#elif defined" block to this file to include support
5 | for a particular motor driver. Then add the appropriate
6 | #define near the top of the main ROSArduinoBridge.ino file.
7 |
8 | *************************************************************/
9 |
10 | #ifdef USE_BASE
11 |
12 | #ifdef POLOLU_VNH5019
13 | /* Include the Pololu library */
14 | #include "DualVNH5019MotorShield.h"
15 |
16 | /* Create the motor driver object */
17 | DualVNH5019MotorShield drive;
18 |
19 | /* Wrap the motor driver initialization */
20 | void initMotorController() {
21 | drive.init();
22 | }
23 |
24 | /* Wrap the drive motor set speed function */
25 | void setMotorSpeed(int i, int spd) {
26 | if (i == LEFT) drive.setM1Speed(spd);
27 | else drive.setM2Speed(spd);
28 | }
29 |
30 | // A convenience function for setting both motor speeds
31 | void setMotorSpeeds(int leftSpeed, int rightSpeed) {
32 | setMotorSpeed(LEFT, leftSpeed);
33 | setMotorSpeed(RIGHT, rightSpeed);
34 | }
35 | #elif defined POLOLU_MC33926
36 | /* Include the Pololu library */
37 | #include "DualMC33926MotorShield.h"
38 |
39 | /* Create the motor driver object */
40 | DualMC33926MotorShield drive;
41 |
42 | /* Wrap the motor driver initialization */
43 | void initMotorController() {
44 | drive.init();
45 | }
46 |
47 | /* Wrap the drive motor set speed function */
48 | void setMotorSpeed(int i, int spd) {
49 | if (i == LEFT) drive.setM1Speed(spd);
50 | else drive.setM2Speed(spd);
51 | }
52 |
53 | // A convenience function for setting both motor speeds
54 | void setMotorSpeeds(int leftSpeed, int rightSpeed) {
55 | setMotorSpeed(LEFT, leftSpeed);
56 | setMotorSpeed(RIGHT, rightSpeed);
57 | }
58 | #elif defined L298_MOTOR_DRIVER
59 | void initMotorController() {
60 | digitalWrite(RIGHT_MOTOR_ENABLE, HIGH);
61 | digitalWrite(LEFT_MOTOR_ENABLE, HIGH);
62 | }
63 |
64 | void setMotorSpeed(int i, int spd) {
65 | unsigned char reverse = 0;
66 |
67 | if (spd < 0)
68 | {
69 | spd = -spd;
70 | reverse = 1;
71 | }
72 | if (spd > 255)
73 | spd = 255;
74 |
75 | if (i == LEFT) {
76 | if (reverse == 0) { analogWrite(RIGHT_MOTOR_FORWARD, spd); analogWrite(RIGHT_MOTOR_BACKWARD, 0); }
77 | else if (reverse == 1) { analogWrite(RIGHT_MOTOR_BACKWARD, spd); analogWrite(RIGHT_MOTOR_FORWARD, 0); }
78 | }
79 | else /*if (i == RIGHT) //no need for condition*/ {
80 | if (reverse == 0) { analogWrite(LEFT_MOTOR_FORWARD, spd); analogWrite(LEFT_MOTOR_BACKWARD, 0); }
81 | else if (reverse == 1) { analogWrite(LEFT_MOTOR_BACKWARD, spd); analogWrite(LEFT_MOTOR_FORWARD, 0); }
82 | }
83 | }
84 |
85 | void setMotorSpeeds(int leftSpeed, int rightSpeed) {
86 | setMotorSpeed(LEFT, leftSpeed);
87 | setMotorSpeed(RIGHT, rightSpeed);
88 | }
89 | #else
90 | #error A motor driver must be selected!
91 | #endif
92 |
93 | #endif
94 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/sensors.h:
--------------------------------------------------------------------------------
1 | /* Functions for various sensor types */
2 |
3 | float microsecondsToCm(long microseconds)
4 | {
5 | // The speed of sound is 340 m/s or 29 microseconds per cm.
6 | // The ping travels out and back, so to find the distance of the
7 | // object we take half of the distance travelled.
8 | return microseconds / 29 / 2;
9 | }
10 |
11 | long Ping(int pin) {
12 | long duration, range;
13 |
14 | // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
15 | // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
16 | pinMode(pin, OUTPUT);
17 | digitalWrite(pin, LOW);
18 | delayMicroseconds(2);
19 | digitalWrite(pin, HIGH);
20 | delayMicroseconds(5);
21 | digitalWrite(pin, LOW);
22 |
23 | // The same pin is used to read the signal from the PING))): a HIGH
24 | // pulse whose duration is the time (in microseconds) from the sending
25 | // of the ping to the reception of its echo off of an object.
26 | pinMode(pin, INPUT);
27 | duration = pulseIn(pin, HIGH);
28 |
29 | // convert the time into meters
30 | range = microsecondsToCm(duration);
31 |
32 | return(range);
33 | }
34 |
35 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/servos.h:
--------------------------------------------------------------------------------
1 | #ifndef SERVOS_H
2 | #define SERVOS_H
3 |
4 |
5 | #define N_SERVOS 2
6 |
7 | // This delay in milliseconds determines the pause
8 | // between each one degree step the servo travels. Increasing
9 | // this number will make the servo sweep more slowly.
10 | // Decreasing this number will make the servo sweep more quickly.
11 | // Zero is the default number and will make the servos spin at
12 | // full speed. 150 ms makes them spin very slowly.
13 | int stepDelay [N_SERVOS] = { 0, 0 }; // ms
14 |
15 | // Pins
16 | byte servoPins [N_SERVOS] = { 3, 4 };
17 |
18 | // Initial Position
19 | byte servoInitPosition [N_SERVOS] = { 90, 90 }; // [0, 180] degrees
20 |
21 |
22 | class SweepServo
23 | {
24 | public:
25 | SweepServo();
26 | void initServo(
27 | int servoPin,
28 | int stepDelayMs,
29 | int initPosition);
30 | void doSweep();
31 | void setTargetPosition(int position);
32 | Servo getServo();
33 |
34 | private:
35 | Servo servo;
36 | int stepDelayMs;
37 | int currentPositionDegrees;
38 | int targetPositionDegrees;
39 | long lastSweepCommand;
40 | };
41 |
42 | SweepServo servos [N_SERVOS];
43 |
44 | #endif
45 |
--------------------------------------------------------------------------------
/ros_arduino_firmware/src/libraries/ROSArduinoBridge/servos.ino:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 | Servo Sweep - by Nathaniel Gallinger
3 |
4 | Sweep servos one degree step at a time with a user defined
5 | delay in between steps. Supports changing direction
6 | mid-sweep. Important for applications such as robotic arms
7 | where the stock servo speed is too fast for the strength
8 | of your system.
9 |
10 | *************************************************************/
11 |
12 | #ifdef USE_SERVOS
13 |
14 |
15 | // Constructor
16 | SweepServo::SweepServo()
17 | {
18 | this->currentPositionDegrees = 0;
19 | this->targetPositionDegrees = 0;
20 | this->lastSweepCommand = 0;
21 | }
22 |
23 |
24 | // Init
25 | void SweepServo::initServo(
26 | int servoPin,
27 | int stepDelayMs,
28 | int initPosition)
29 | {
30 | this->servo.attach(servoPin);
31 | this->stepDelayMs = stepDelayMs;
32 | this->currentPositionDegrees = initPosition;
33 | this->targetPositionDegrees = initPosition;
34 | this->lastSweepCommand = millis();
35 | }
36 |
37 |
38 | // Perform Sweep
39 | void SweepServo::doSweep()
40 | {
41 |
42 | // Get ellapsed time
43 | int delta = millis() - this->lastSweepCommand;
44 |
45 | // Check if time for a step
46 | if (delta > this->stepDelayMs) {
47 | // Check step direction
48 | if (this->targetPositionDegrees > this->currentPositionDegrees) {
49 | this->currentPositionDegrees++;
50 | this->servo.write(this->currentPositionDegrees);
51 | }
52 | else if (this->targetPositionDegrees < this->currentPositionDegrees) {
53 | this->currentPositionDegrees--;
54 | this->servo.write(this->currentPositionDegrees);
55 | }
56 | // if target == current position, do nothing
57 |
58 | // reset timer
59 | this->lastSweepCommand = millis();
60 | }
61 | }
62 |
63 |
64 | // Set a new target position
65 | void SweepServo::setTargetPosition(int position)
66 | {
67 | this->targetPositionDegrees = position;
68 | }
69 |
70 |
71 | // Accessor for servo object
72 | Servo SweepServo::getServo()
73 | {
74 | return this->servo;
75 | }
76 |
77 |
78 | #endif
79 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.8.3)
2 | project(ros_arduino_msgs)
3 |
4 | find_package(catkin REQUIRED COMPONENTS std_msgs message_generation)
5 |
6 | add_message_files(FILES
7 | AnalogFloat.msg
8 | Analog.msg
9 | ArduinoConstants.msg
10 | Digital.msg
11 | SensorState.msg
12 | )
13 |
14 | add_service_files(FILES
15 | DigitalSetDirection.srv
16 | DigitalWrite.srv
17 | DigitalRead.srv
18 | ServoRead.srv
19 | ServoWrite.srv
20 | AnalogWrite.srv
21 | AnalogRead.srv
22 | )
23 |
24 | generate_messages(
25 | DEPENDENCIES
26 | std_msgs
27 | )
28 |
29 | catkin_package(CATKIN_DEPENDS message_runtime std_msgs)
30 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/msg/Analog.msg:
--------------------------------------------------------------------------------
1 | # Reading from a single analog IO pin.
2 | Header header
3 | uint16 value
4 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/msg/AnalogFloat.msg:
--------------------------------------------------------------------------------
1 | # Float message from a single analog IO pin.
2 | Header header
3 | float64 value
--------------------------------------------------------------------------------
/ros_arduino_msgs/msg/ArduinoConstants.msg:
--------------------------------------------------------------------------------
1 | # Arduino-style constants
2 | uint8 LOW=0
3 | uint8 HIGH=1
4 | uint8 INPUT=0
5 | uint8 OUTPUT=1
6 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/msg/Digital.msg:
--------------------------------------------------------------------------------
1 | # Reading on a digital pin
2 | Header header
3 | uint8 value
4 |
5 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/msg/SensorState.msg:
--------------------------------------------------------------------------------
1 | Header header
2 |
3 | string[] name
4 | float32[] value
5 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/package.xml:
--------------------------------------------------------------------------------
1 |
2 | ros_arduino_msgs
3 | 0.2.0
4 |
5 | ROS Arduino Messages.
6 |
7 | Patrick Goebel
8 | Patrick Goebel
9 | BSD
10 | http://ros.org/wiki/ros_arduino_msgs
11 |
12 | catkin
13 |
14 | message_generation
15 | std_msgs
16 |
17 | message_runtime
18 | std_msgs
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/srv/AnalogRead.srv:
--------------------------------------------------------------------------------
1 | uint8 pin
2 | ---
3 | uint16 value
4 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/srv/AnalogWrite.srv:
--------------------------------------------------------------------------------
1 | uint8 pin
2 | uint16 value
3 | ---
4 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/srv/DigitalRead.srv:
--------------------------------------------------------------------------------
1 | uint8 pin
2 | ---
3 | bool value
4 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/srv/DigitalSetDirection.srv:
--------------------------------------------------------------------------------
1 | uint8 pin
2 | bool direction
3 | ---
4 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/srv/DigitalWrite.srv:
--------------------------------------------------------------------------------
1 | uint8 pin
2 | bool value
3 | ---
4 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/srv/ServoRead.srv:
--------------------------------------------------------------------------------
1 | uint8 id
2 | ---
3 | float32 value
4 |
--------------------------------------------------------------------------------
/ros_arduino_msgs/srv/ServoWrite.srv:
--------------------------------------------------------------------------------
1 | uint8 id
2 | float32 value
3 | ---
4 |
--------------------------------------------------------------------------------
/ros_arduino_python/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.8.3)
2 | project(ros_arduino_python)
3 |
4 | find_package(catkin REQUIRED)
5 | catkin_package(DEPENDS)
6 | catkin_python_setup()
7 |
8 | install(DIRECTORY config
9 | DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
10 | )
11 |
12 | install(DIRECTORY launch
13 | DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
14 | )
15 |
16 | install(DIRECTORY nodes
17 | DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
18 | )
19 |
--------------------------------------------------------------------------------
/ros_arduino_python/config/arduino_params.yaml:
--------------------------------------------------------------------------------
1 | # For a direct USB cable connection, the port name is typically
2 | # /dev/ttyACM# where is # is a number such as 0, 1, 2, etc
3 | # For a wireless connection like XBee, the port is typically
4 | # /dev/ttyUSB# where # is a number such as 0, 1, 2, etc.
5 |
6 | port: /dev/ttyACM0
7 | baud: 57600
8 | timeout: 0.1
9 |
10 | rate: 50
11 | sensorstate_rate: 10
12 |
13 | use_base_controller: False
14 | base_controller_rate: 10
15 |
16 | # For a robot that uses base_footprint, change base_frame to base_footprint
17 | base_frame: base_link
18 |
19 | # === Robot drivetrain parameters
20 | #wheel_diameter: 0.146
21 | #wheel_track: 0.2969
22 | #encoder_resolution: 8384 # from Pololu for 131:1 motors
23 | #gear_reduction: 1.0
24 | #motors_reversed: True
25 |
26 | # === PID parameters
27 | #Kp: 10
28 | #Kd: 12
29 | #Ki: 0
30 | #Ko: 50
31 | #accel_limit: 1.0
32 |
33 | # === Sensor definitions. Examples only - edit for your robot.
34 | # Sensor type can be one of the follow (case sensitive!):
35 | # * Ping
36 | # * GP2D12
37 | # * Analog
38 | # * Digital
39 | # * PololuMotorCurrent
40 | # * PhidgetsVoltage
41 | # * PhidgetsCurrent (20 Amp, DC)
42 |
43 |
44 |
45 | sensors: {
46 | #motor_current_left: {pin: 0, type: PololuMotorCurrent, rate: 5},
47 | #motor_current_right: {pin: 1, type: PololuMotorCurrent, rate: 5},
48 | #ir_front_center: {pin: 2, type: GP2D12, rate: 10},
49 | #sonar_front_center: {pin: 5, type: Ping, rate: 10},
50 | arduino_led: {pin: 13, type: Digital, rate: 5, direction: output}
51 | }
52 |
--------------------------------------------------------------------------------
/ros_arduino_python/launch/arduino.launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/ros_arduino_python/nodes/arduino_node.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | """
4 | A ROS Node for the Arduino microcontroller
5 |
6 | Created for the Pi Robot Project: http://www.pirobot.org
7 | Copyright (c) 2012 Patrick Goebel. All rights reserved.
8 |
9 | This program is free software; you can redistribute it and/or modify
10 | it under the terms of the GNU General Public License as published by
11 | the Free Software Foundation; either version 2 of the License, or
12 | (at your option) any later version.
13 |
14 | This program is distributed in the hope that it will be useful,
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | GNU General Public License for more details at:
18 |
19 | http://www.gnu.org/licenses/gpl.html
20 | """
21 |
22 | import rospy
23 | from ros_arduino_python.arduino_driver import Arduino
24 | from ros_arduino_python.arduino_sensors import *
25 | from ros_arduino_msgs.srv import *
26 | from ros_arduino_python.base_controller import BaseController
27 | from geometry_msgs.msg import Twist
28 | import os, time
29 | import thread
30 | from serial.serialutil import SerialException
31 |
32 | class ArduinoROS():
33 | def __init__(self):
34 | rospy.init_node('arduino', log_level=rospy.INFO)
35 |
36 | # Get the actual node name in case it is set in the launch file
37 | self.name = rospy.get_name()
38 |
39 | # Cleanup when termniating the node
40 | rospy.on_shutdown(self.shutdown)
41 |
42 | self.port = rospy.get_param("~port", "/dev/ttyACM0")
43 | self.baud = int(rospy.get_param("~baud", 57600))
44 | self.timeout = rospy.get_param("~timeout", 0.5)
45 | self.base_frame = rospy.get_param("~base_frame", 'base_link')
46 | self.motors_reversed = rospy.get_param("~motors_reversed", False)
47 | # Overall loop rate: should be faster than fastest sensor rate
48 | self.rate = int(rospy.get_param("~rate", 50))
49 | r = rospy.Rate(self.rate)
50 |
51 | # Rate at which summary SensorState message is published. Individual sensors publish
52 | # at their own rates.
53 | self.sensorstate_rate = int(rospy.get_param("~sensorstate_rate", 10))
54 |
55 | self.use_base_controller = rospy.get_param("~use_base_controller", False)
56 |
57 | # Set up the time for publishing the next SensorState message
58 | now = rospy.Time.now()
59 | self.t_delta_sensors = rospy.Duration(1.0 / self.sensorstate_rate)
60 | self.t_next_sensors = now + self.t_delta_sensors
61 |
62 | # Initialize a Twist message
63 | self.cmd_vel = Twist()
64 |
65 | # A cmd_vel publisher so we can stop the robot when shutting down
66 | self.cmd_vel_pub = rospy.Publisher('cmd_vel', Twist, queue_size=5)
67 |
68 | # The SensorState publisher periodically publishes the values of all sensors on
69 | # a single topic.
70 | self.sensorStatePub = rospy.Publisher('~sensor_state', SensorState, queue_size=5)
71 |
72 | # A service to position a PWM servo
73 | rospy.Service('~servo_write', ServoWrite, self.ServoWriteHandler)
74 |
75 | # A service to read the position of a PWM servo
76 | rospy.Service('~servo_read', ServoRead, self.ServoReadHandler)
77 |
78 | # A service to turn set the direction of a digital pin (0 = input, 1 = output)
79 | rospy.Service('~digital_set_direction', DigitalSetDirection, self.DigitalSetDirectionHandler)
80 |
81 | # A service to turn a digital sensor on or off
82 | rospy.Service('~digital_write', DigitalWrite, self.DigitalWriteHandler)
83 |
84 | # A service to read the value of a digital sensor
85 | rospy.Service('~digital_read', DigitalRead, self.DigitalReadHandler)
86 |
87 | # A service to set pwm values for the pins
88 | rospy.Service('~analog_write', AnalogWrite, self.AnalogWriteHandler)
89 |
90 | # A service to read the value of an analog sensor
91 | rospy.Service('~analog_read', AnalogRead, self.AnalogReadHandler)
92 |
93 | # Initialize the controlller
94 | self.controller = Arduino(self.port, self.baud, self.timeout, self.motors_reversed)
95 |
96 | # Make the connection
97 | self.controller.connect()
98 |
99 | rospy.loginfo("Connected to Arduino on port " + self.port + " at " + str(self.baud) + " baud")
100 |
101 | # Reserve a thread lock
102 | mutex = thread.allocate_lock()
103 |
104 | # Initialize any sensors
105 | self.mySensors = list()
106 |
107 | sensor_params = rospy.get_param("~sensors", dict({}))
108 |
109 | for name, params in sensor_params.iteritems():
110 | # Set the direction to input if not specified
111 | try:
112 | params['direction']
113 | except:
114 | params['direction'] = 'input'
115 |
116 | if params['type'] == "Ping":
117 | sensor = Ping(self.controller, name, params['pin'], params['rate'], self.base_frame)
118 | elif params['type'] == "GP2D12":
119 | sensor = GP2D12(self.controller, name, params['pin'], params['rate'], self.base_frame)
120 | elif params['type'] == 'Digital':
121 | sensor = DigitalSensor(self.controller, name, params['pin'], params['rate'], self.base_frame, direction=params['direction'])
122 | elif params['type'] == 'Analog':
123 | sensor = AnalogSensor(self.controller, name, params['pin'], params['rate'], self.base_frame, direction=params['direction'])
124 | elif params['type'] == 'PololuMotorCurrent':
125 | sensor = PololuMotorCurrent(self.controller, name, params['pin'], params['rate'], self.base_frame)
126 | elif params['type'] == 'PhidgetsVoltage':
127 | sensor = PhidgetsVoltage(self.controller, name, params['pin'], params['rate'], self.base_frame)
128 | elif params['type'] == 'PhidgetsCurrent':
129 | sensor = PhidgetsCurrent(self.controller, name, params['pin'], params['rate'], self.base_frame)
130 |
131 | # if params['type'] == "MaxEZ1":
132 | # self.sensors[len(self.sensors)]['trigger_pin'] = params['trigger_pin']
133 | # self.sensors[len(self.sensors)]['output_pin'] = params['output_pin']
134 | try:
135 | self.mySensors.append(sensor)
136 | rospy.loginfo(name + " " + str(params) + " published on topic " + rospy.get_name() + "/sensor/" + name)
137 | except:
138 | rospy.logerr("Sensor type " + str(params['type']) + " not recognized.")
139 |
140 | # Initialize the base controller if used
141 | if self.use_base_controller:
142 | self.myBaseController = BaseController(self.controller, self.base_frame, self.name + "_base_controller")
143 |
144 | # Start polling the sensors and base controller
145 | while not rospy.is_shutdown():
146 | for sensor in self.mySensors:
147 | mutex.acquire()
148 | sensor.poll()
149 | mutex.release()
150 |
151 | if self.use_base_controller:
152 | mutex.acquire()
153 | self.myBaseController.poll()
154 | mutex.release()
155 |
156 | # Publish all sensor values on a single topic for convenience
157 | now = rospy.Time.now()
158 |
159 | if now > self.t_next_sensors:
160 | msg = SensorState()
161 | msg.header.frame_id = self.base_frame
162 | msg.header.stamp = now
163 | for i in range(len(self.mySensors)):
164 | msg.name.append(self.mySensors[i].name)
165 | msg.value.append(self.mySensors[i].value)
166 | try:
167 | self.sensorStatePub.publish(msg)
168 | except:
169 | pass
170 |
171 | self.t_next_sensors = now + self.t_delta_sensors
172 |
173 | r.sleep()
174 |
175 | # Service callback functions
176 | def ServoWriteHandler(self, req):
177 | self.controller.servo_write(req.id, req.value)
178 | return ServoWriteResponse()
179 |
180 | def ServoReadHandler(self, req):
181 | pos = self.controller.servo_read(req.id)
182 | return ServoReadResponse(pos)
183 |
184 | def DigitalSetDirectionHandler(self, req):
185 | self.controller.pin_mode(req.pin, req.direction)
186 | return DigitalSetDirectionResponse()
187 |
188 | def DigitalWriteHandler(self, req):
189 | self.controller.digital_write(req.pin, req.value)
190 | return DigitalWriteResponse()
191 |
192 | def DigitalReadHandler(self, req):
193 | value = self.controller.digital_read(req.pin)
194 | return DigitalReadResponse(value)
195 |
196 | def AnalogWriteHandler(self, req):
197 | self.controller.analog_write(req.pin, req.value)
198 | return AnalogWriteResponse()
199 |
200 | def AnalogReadHandler(self, req):
201 | value = self.controller.analog_read(req.pin)
202 | return AnalogReadResponse(value)
203 |
204 | def shutdown(self):
205 | rospy.loginfo("Shutting down Arduino Node...")
206 |
207 | # Stop the robot
208 | try:
209 | rospy.loginfo("Stopping the robot...")
210 | self.cmd_vel_pub.Publish(Twist())
211 | rospy.sleep(2)
212 | except:
213 | pass
214 |
215 | # Close the serial port
216 | try:
217 | self.controller.close()
218 | except:
219 | pass
220 | finally:
221 | rospy.loginfo("Serial port closed.")
222 | os._exit(0)
223 |
224 | if __name__ == '__main__':
225 | try:
226 | myArduino = ArduinoROS()
227 | except SerialException:
228 | rospy.logerr("Serial exception trying to open port.")
229 | os._exit(0)
230 |
--------------------------------------------------------------------------------
/ros_arduino_python/package.xml:
--------------------------------------------------------------------------------
1 |
2 | ros_arduino_python
3 | 0.2.0
4 |
5 | ROS Arduino Python.
6 |
7 | Patrick Goebel
8 | Patrick Goebel
9 | BSD
10 | http://ros.org/wiki/ros_arduino_python
11 |
12 | catkin
13 |
14 | rospy
15 | std_msgs
16 | sensor_msgs
17 | geometry_msgs
18 | nav_msgs
19 | tf
20 | ros_arduino_msgs
21 | python-serial
22 |
23 |
--------------------------------------------------------------------------------
/ros_arduino_python/setup.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | from distutils.core import setup
4 | from catkin_pkg.python_setup import generate_distutils_setup
5 |
6 | d = generate_distutils_setup(
7 | packages=['ros_arduino_python'],
8 | package_dir={'': 'src'},
9 | )
10 |
11 | setup(**d)
12 |
--------------------------------------------------------------------------------
/ros_arduino_python/src/ros_arduino_python/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hbrobotics/ros_arduino_bridge/a960c8a88a6255d0104c92838045c06257c509d0/ros_arduino_python/src/ros_arduino_python/__init__.py
--------------------------------------------------------------------------------
/ros_arduino_python/src/ros_arduino_python/arduino_driver.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | """
4 | A Python driver for the Arduino microcontroller running the
5 | ROSArduinoBridge firmware.
6 |
7 | Created for the Pi Robot Project: http://www.pirobot.org
8 | Copyright (c) 2012 Patrick Goebel. All rights reserved.
9 |
10 | This program is free software; you can redistribute it and/or modify
11 | it under the terms of the GNU General Public License as published by
12 | the Free Software Foundation; either version 2 of the License, or
13 | (at your option) any later version.
14 |
15 | This program is distributed in the hope that it will be useful,
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 | GNU General Public License for more details at:
19 |
20 | http://www.gnu.org/licenses/gpl.html
21 |
22 | """
23 |
24 | import thread
25 | from math import pi as PI, degrees, radians
26 | import os
27 | import time
28 | import sys, traceback
29 | from serial.serialutil import SerialException
30 | from serial import Serial
31 |
32 | SERVO_MAX = 180
33 | SERVO_MIN = 0
34 |
35 | class Arduino:
36 | ''' Configuration Parameters
37 | '''
38 | N_ANALOG_PORTS = 6
39 | N_DIGITAL_PORTS = 12
40 |
41 | def __init__(self, port="/dev/ttyUSB0", baudrate=57600, timeout=0.5, motors_reversed=False):
42 |
43 | self.PID_RATE = 30 # Do not change this! It is a fixed property of the Arduino PID controller.
44 | self.PID_INTERVAL = 1000 / 30
45 |
46 | self.port = port
47 | self.baudrate = baudrate
48 | self.timeout = timeout
49 | self.encoder_count = 0
50 | self.writeTimeout = timeout
51 | self.interCharTimeout = timeout / 30.
52 | self.motors_reversed = motors_reversed
53 | # Keep things thread safe
54 | self.mutex = thread.allocate_lock()
55 |
56 | # An array to cache analog sensor readings
57 | self.analog_sensor_cache = [None] * self.N_ANALOG_PORTS
58 |
59 | # An array to cache digital sensor readings
60 | self.digital_sensor_cache = [None] * self.N_DIGITAL_PORTS
61 |
62 | def connect(self):
63 | try:
64 | print "Connecting to Arduino on port", self.port, "..."
65 | self.port = Serial(port=self.port, baudrate=self.baudrate, timeout=self.timeout, writeTimeout=self.writeTimeout)
66 | # The next line is necessary to give the firmware time to wake up.
67 | time.sleep(1)
68 | test = self.get_baud()
69 | if test != self.baudrate:
70 | time.sleep(1)
71 | test = self.get_baud()
72 | if test != self.baudrate:
73 | raise SerialException
74 | print "Connected at", self.baudrate
75 | print "Arduino is ready."
76 |
77 | except SerialException:
78 | print "Serial Exception:"
79 | print sys.exc_info()
80 | print "Traceback follows:"
81 | traceback.print_exc(file=sys.stdout)
82 | print "Cannot connect to Arduino!"
83 | os._exit(1)
84 |
85 | def open(self):
86 | ''' Open the serial port.
87 | '''
88 | self.port.open()
89 |
90 | def close(self):
91 | ''' Close the serial port.
92 | '''
93 | self.port.close()
94 |
95 | def send(self, cmd):
96 | ''' This command should not be used on its own: it is called by the execute commands
97 | below in a thread safe manner.
98 | '''
99 | self.port.write(cmd + '\r')
100 |
101 | def recv(self, timeout=0.5):
102 | timeout = min(timeout, self.timeout)
103 | ''' This command should not be used on its own: it is called by the execute commands
104 | below in a thread safe manner. Note: we use read() instead of readline() since
105 | readline() tends to return garbage characters from the Arduino
106 | '''
107 | c = ''
108 | value = ''
109 | attempts = 0
110 | while c != '\r':
111 | c = self.port.read(1)
112 | value += c
113 | attempts += 1
114 | if attempts * self.interCharTimeout > timeout:
115 | return None
116 |
117 | value = value.strip('\r')
118 |
119 | return value
120 |
121 | def recv_ack(self):
122 | ''' This command should not be used on its own: it is called by the execute commands
123 | below in a thread safe manner.
124 | '''
125 | ack = self.recv(self.timeout)
126 | return ack == 'OK'
127 |
128 | def recv_int(self):
129 | ''' This command should not be used on its own: it is called by the execute commands
130 | below in a thread safe manner.
131 | '''
132 | value = self.recv(self.timeout)
133 | try:
134 | return int(value)
135 | except:
136 | return None
137 |
138 | def recv_array(self):
139 | ''' This command should not be used on its own: it is called by the execute commands
140 | below in a thread safe manner.
141 | '''
142 | try:
143 | values = self.recv(self.timeout * self.N_ANALOG_PORTS).split()
144 | return map(int, values)
145 | except:
146 | return []
147 |
148 | def execute(self, cmd):
149 | ''' Thread safe execution of "cmd" on the Arduino returning a single integer value.
150 | '''
151 | self.mutex.acquire()
152 |
153 | try:
154 | self.port.flushInput()
155 | except:
156 | pass
157 |
158 | ntries = 1
159 | attempts = 0
160 |
161 | try:
162 | self.port.write(cmd + '\r')
163 | value = self.recv(self.timeout)
164 | while attempts < ntries and (value == '' or value == 'Invalid Command' or value == None):
165 | try:
166 | self.port.flushInput()
167 | self.port.write(cmd + '\r')
168 | value = self.recv(self.timeout)
169 | except:
170 | print "Exception executing command: " + cmd
171 | attempts += 1
172 | except:
173 | self.mutex.release()
174 | print "Exception executing command: " + cmd
175 | value = None
176 |
177 | self.mutex.release()
178 | return int(value)
179 |
180 | def execute_array(self, cmd):
181 | ''' Thread safe execution of "cmd" on the Arduino returning an array.
182 | '''
183 | self.mutex.acquire()
184 |
185 | try:
186 | self.port.flushInput()
187 | except:
188 | pass
189 |
190 | ntries = 1
191 | attempts = 0
192 |
193 | try:
194 | self.port.write(cmd + '\r')
195 | values = self.recv_array()
196 | while attempts < ntries and (values == '' or values == 'Invalid Command' or values == [] or values == None):
197 | try:
198 | self.port.flushInput()
199 | self.port.write(cmd + '\r')
200 | values = self.recv_array()
201 | except:
202 | print("Exception executing command: " + cmd)
203 | attempts += 1
204 | except:
205 | self.mutex.release()
206 | print "Exception executing command: " + cmd
207 | raise SerialException
208 | return []
209 |
210 | try:
211 | values = map(int, values)
212 | except:
213 | values = []
214 |
215 | self.mutex.release()
216 | return values
217 |
218 | def execute_ack(self, cmd):
219 | ''' Thread safe execution of "cmd" on the Arduino returning True if response is ACK.
220 | '''
221 | self.mutex.acquire()
222 |
223 | try:
224 | self.port.flushInput()
225 | except:
226 | pass
227 |
228 | ntries = 1
229 | attempts = 0
230 |
231 | try:
232 | self.port.write(cmd + '\r')
233 | ack = self.recv(self.timeout)
234 | while attempts < ntries and (ack == '' or ack == 'Invalid Command' or ack == None):
235 | try:
236 | self.port.flushInput()
237 | self.port.write(cmd + '\r')
238 | ack = self.recv(self.timeout)
239 | except:
240 | print "Exception executing command: " + cmd
241 | attempts += 1
242 | except:
243 | self.mutex.release()
244 | print "execute_ack exception when executing", cmd
245 | print sys.exc_info()
246 | return 0
247 |
248 | self.mutex.release()
249 | return ack == 'OK'
250 |
251 | def update_pid(self, Kp, Kd, Ki, Ko):
252 | ''' Set the PID parameters on the Arduino
253 | '''
254 | print "Updating PID parameters"
255 | cmd = 'u ' + str(Kp) + ':' + str(Kd) + ':' + str(Ki) + ':' + str(Ko)
256 | self.execute_ack(cmd)
257 |
258 | def get_baud(self):
259 | ''' Get the current baud rate on the serial port.
260 | '''
261 | try:
262 | return int(self.execute('b'));
263 | except:
264 | return None
265 |
266 | def get_encoder_counts(self):
267 | values = self.execute_array('e')
268 | if len(values) != 2:
269 | print "Encoder count was not 2"
270 | raise SerialException
271 | return None
272 | else:
273 | if self.motors_reversed:
274 | values[0], values[1] = -values[0], -values[1]
275 | return values
276 |
277 | def reset_encoders(self):
278 | ''' Reset the encoder counts to 0
279 | '''
280 | return self.execute_ack('r')
281 |
282 | def drive(self, right, left):
283 | ''' Speeds are given in encoder ticks per PID interval
284 | '''
285 | if self.motors_reversed:
286 | left, right = -left, -right
287 | return self.execute_ack('m %d %d' %(right, left))
288 |
289 | def drive_m_per_s(self, right, left):
290 | ''' Set the motor speeds in meters per second.
291 | '''
292 | left_revs_per_second = float(left) / (self.wheel_diameter * PI)
293 | right_revs_per_second = float(right) / (self.wheel_diameter * PI)
294 |
295 | left_ticks_per_loop = int(left_revs_per_second * self.encoder_resolution * self.PID_INTERVAL * self.gear_reduction)
296 | right_ticks_per_loop = int(right_revs_per_second * self.encoder_resolution * self.PID_INTERVAL * self.gear_reduction)
297 |
298 | self.drive(right_ticks_per_loop , left_ticks_per_loop )
299 |
300 | def stop(self):
301 | ''' Stop both motors.
302 | '''
303 | self.drive(0, 0)
304 |
305 | def analog_read(self, pin):
306 | return self.execute('a %d' %pin)
307 |
308 | def analog_write(self, pin, value):
309 | return self.execute_ack('x %d %d' %(pin, value))
310 |
311 | def digital_read(self, pin):
312 | return self.execute('d %d' %pin)
313 |
314 | def digital_write(self, pin, value):
315 | return self.execute_ack('w %d %d' %(pin, value))
316 |
317 | def pin_mode(self, pin, mode):
318 | return self.execute_ack('c %d %d' %(pin, mode))
319 |
320 | def servo_write(self, id, pos):
321 | ''' Usage: servo_write(id, pos)
322 | Position is given in radians and converted to degrees before sending
323 | '''
324 | return self.execute_ack('s %d %d' %(id, min(SERVO_MAX, max(SERVO_MIN, degrees(pos)))))
325 |
326 | def servo_read(self, id):
327 | ''' Usage: servo_read(id)
328 | The returned position is converted from degrees to radians
329 | '''
330 | return radians(self.execute('t %d' %id))
331 |
332 | def ping(self, pin):
333 | ''' The srf05/Ping command queries an SRF05/Ping sonar sensor
334 | connected to the General Purpose I/O line pinId for a distance,
335 | and returns the range in cm. Sonar distance resolution is integer based.
336 | '''
337 | return self.execute('p %d' %pin);
338 |
339 | # def get_maxez1(self, triggerPin, outputPin):
340 | # ''' The maxez1 command queries a Maxbotix MaxSonar-EZ1 sonar
341 | # sensor connected to the General Purpose I/O lines, triggerPin, and
342 | # outputPin, for a distance, and returns it in Centimeters. NOTE: MAKE
343 | # SURE there's nothing directly in front of the MaxSonar-EZ1 upon
344 | # power up, otherwise it wont range correctly for object less than 6
345 | # inches away! The sensor reading defaults to use English units
346 | # (inches). The sensor distance resolution is integer based. Also, the
347 | # maxsonar trigger pin is RX, and the echo pin is PW.
348 | # '''
349 | # return self.execute('z %d %d' %(triggerPin, outputPin))
350 |
351 |
352 | """ Basic test for connectivity """
353 | if __name__ == "__main__":
354 | if os.name == "posix":
355 | portName = "/dev/ttyACM0"
356 | else:
357 | portName = "COM43" # Windows style COM port.
358 |
359 | baudRate = 57600
360 |
361 | myArduino = Arduino(port=portName, baudrate=baudRate, timeout=0.5)
362 | myArduino.connect()
363 |
364 | print "Sleeping for 1 second..."
365 | time.sleep(1)
366 |
367 | print "Reading on analog port 0", myArduino.analog_read(0)
368 | print "Reading on digital port 0", myArduino.digital_read(0)
369 | print "Blinking the LED 3 times"
370 | for i in range(3):
371 | myArduino.digital_write(13, 1)
372 | time.sleep(1.0)
373 | #print "Current encoder counts", myArduino.encoders()
374 |
375 | print "Connection test successful.",
376 |
377 | myArduino.stop()
378 | myArduino.close()
379 |
380 | print "Shutting down Arduino."
381 |
--------------------------------------------------------------------------------
/ros_arduino_python/src/ros_arduino_python/arduino_sensors.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | """
4 | Sensor class for the arudino_python package
5 |
6 | Created for the Pi Robot Project: http://www.pirobot.org
7 | Copyright (c) 2012 Patrick Goebel. All rights reserved.
8 |
9 | This program is free software; you can redistribute it and/or modify
10 | it under the terms of the GNU General Public License as published by
11 | the Free Software Foundation; either version 2 of the License, or
12 | (at your option) any later version.
13 |
14 | This program is distributed in the hope that it will be useful,
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | GNU General Public License for more details at:
18 |
19 | http://www.gnu.org/licenses/gpl.html
20 | """
21 |
22 | import roslib; roslib.load_manifest('ros_arduino_python')
23 | import rospy
24 | from sensor_msgs.msg import Range
25 | from ros_arduino_msgs.msg import *
26 |
27 | LOW = 0
28 | HIGH = 1
29 |
30 | INPUT = 0
31 | OUTPUT = 1
32 |
33 | class MessageType:
34 | ANALOG = 0
35 | DIGITAL = 1
36 | RANGE = 2
37 | FLOAT = 3
38 | INT = 4
39 | BOOL = 5
40 |
41 | class Sensor(object):
42 | def __init__(self, controller, name, pin, rate, frame_id, direction="input", **kwargs):
43 | self.controller = controller
44 | self.name = name
45 | self.pin = pin
46 | self.rate = rate
47 | self.direction = direction
48 |
49 | self.frame_id = frame_id
50 | self.value = None
51 |
52 | self.t_delta = rospy.Duration(1.0 / self.rate)
53 | self.t_next = rospy.Time.now() + self.t_delta
54 |
55 | def poll(self):
56 | now = rospy.Time.now()
57 | if now > self.t_next:
58 | if self.direction == "input":
59 | try:
60 | self.value = self.read_value()
61 | except:
62 | return
63 | else:
64 | try:
65 | self.ack = self.write_value()
66 | except:
67 | return
68 |
69 | # For range sensors, assign the value to the range message field
70 | if self.message_type == MessageType.RANGE:
71 | self.msg.range = self.value
72 | else:
73 | self.msg.value = self.value
74 |
75 | # Add a timestamp and publish the message
76 | self.msg.header.stamp = rospy.Time.now()
77 | self.pub.publish(self.msg)
78 |
79 | self.t_next = now + self.t_delta
80 |
81 | class AnalogSensor(Sensor):
82 | def __init__(self, *args, **kwargs):
83 | super(AnalogSensor, self).__init__(*args, **kwargs)
84 |
85 | self.message_type = MessageType.ANALOG
86 |
87 | self.msg = Analog()
88 | self.msg.header.frame_id = self.frame_id
89 |
90 | self.pub = rospy.Publisher("~sensor/" + self.name, Analog, queue_size=5)
91 |
92 | if self.direction == "output":
93 | self.controller.pin_mode(self.pin, OUTPUT)
94 | else:
95 | self.controller.pin_mode(self.pin, INPUT)
96 |
97 | self.value = LOW
98 |
99 | def read_value(self):
100 | return self.controller.analog_read(self.pin)
101 |
102 | def write_value(self, value):
103 | return self.controller.analog_write(self.pin, value)
104 |
105 | class AnalogFloatSensor(Sensor):
106 | def __init__(self, *args, **kwargs):
107 | super(AnalogFloatSensor, self).__init__(*args, **kwargs)
108 |
109 | self.message_type = MessageType.ANALOG
110 |
111 | self.msg = AnalogFloat()
112 | self.msg.header.frame_id = self.frame_id
113 |
114 | self.pub = rospy.Publisher("~sensor/" + self.name, AnalogFloat, queue_size=5)
115 |
116 | if self.direction == "output":
117 | self.controller.pin_mode(self.pin, OUTPUT)
118 | else:
119 | self.controller.pin_mode(self.pin, INPUT)
120 |
121 | self.value = LOW
122 |
123 | def read_value(self):
124 | return self.controller.analog_read(self.pin)
125 |
126 | def write_value(self, value):
127 | return self.controller.analog_write(self.pin, value)
128 |
129 |
130 | class DigitalSensor(Sensor):
131 | def __init__(self, *args, **kwargs):
132 | super(DigitalSensor, self).__init__(*args, **kwargs)
133 |
134 | self.message_type = MessageType.BOOL
135 |
136 | self.msg = Digital()
137 | self.msg.header.frame_id = self.frame_id
138 |
139 | self.pub = rospy.Publisher("~sensor/" + self.name, Digital, queue_size=5)
140 |
141 | if self.direction == "output":
142 | self.controller.pin_mode(self.pin, OUTPUT)
143 | else:
144 | self.controller.pin_mode(self.pin, INPUT)
145 |
146 | self.value = LOW
147 |
148 | def read_value(self):
149 | return self.controller.digital_read(self.pin)
150 |
151 | def write_value(self):
152 | # Alternate HIGH/LOW when writing at a fixed rate
153 | self.value = not self.value
154 | return self.controller.digital_write(self.pin, self.value)
155 |
156 |
157 | class RangeSensor(Sensor):
158 | def __init__(self, *args, **kwargs):
159 | super(RangeSensor, self).__init__(*args, **kwargs)
160 |
161 | self.message_type = MessageType.RANGE
162 |
163 | self.msg = Range()
164 | self.msg.header.frame_id = self.frame_id
165 |
166 | self.pub = rospy.Publisher("~sensor/" + self.name, Range, queue_size=5)
167 |
168 | def read_value(self):
169 | self.msg.header.stamp = rospy.Time.now()
170 |
171 |
172 | class SonarSensor(RangeSensor):
173 | def __init__(self, *args, **kwargs):
174 | super(SonarSensor, self).__init__(*args, **kwargs)
175 |
176 | self.msg.radiation_type = Range.ULTRASOUND
177 |
178 |
179 | class IRSensor(RangeSensor):
180 | def __init__(self, *args, **kwargs):
181 | super(IRSensor, self).__init__(*args, **kwargs)
182 |
183 | self.msg.radiation_type = Range.INFRARED
184 |
185 | class Ping(SonarSensor):
186 | def __init__(self,*args, **kwargs):
187 | super(Ping, self).__init__(*args, **kwargs)
188 |
189 | self.msg.field_of_view = 0.785398163
190 | self.msg.min_range = 0.02
191 | self.msg.max_range = 3.0
192 |
193 | def read_value(self):
194 | # The Arduino Ping code returns the distance in centimeters
195 | cm = self.controller.ping(self.pin)
196 |
197 | # Convert it to meters for ROS
198 | distance = cm / 100.0
199 |
200 | return distance
201 |
202 |
203 | class GP2D12(IRSensor):
204 | def __init__(self, *args, **kwargs):
205 | super(GP2D12, self).__init__(*args, **kwargs)
206 |
207 | self.msg.field_of_view = 0.001
208 | self.msg.min_range = 0.10
209 | self.msg.max_range = 0.80
210 |
211 | def read_value(self):
212 | value = self.controller.analog_read(self.pin)
213 |
214 | if value <= 3.0:
215 | return self.msg.max_range
216 |
217 | try:
218 | distance = (6787.0 / (float(value) - 3.0)) - 4.0
219 | except:
220 | return self.msg.max_range
221 |
222 | # Convert to meters
223 | distance /= 100.0
224 |
225 | # If we get a spurious reading, set it to the max_range
226 | if distance > self.msg.max_range: distance = self.msg.max_range
227 | if distance < self.msg.min_range: distance = self.msg.max_range
228 |
229 | return distance
230 |
231 | class PololuMotorCurrent(AnalogFloatSensor):
232 | def __init__(self, *args, **kwargs):
233 | super(PololuMotorCurrent, self).__init__(*args, **kwargs)
234 |
235 | def read_value(self):
236 | # From the Pololu source code
237 | milliamps = self.controller.analog_read(self.pin) * 34
238 | return milliamps / 1000.0
239 |
240 | class PhidgetsVoltage(AnalogFloatSensor):
241 | def __init__(self, *args, **kwargs):
242 | super(PhidgetsVoltage, self).__init__(*args, **kwargs)
243 |
244 | def read_value(self):
245 | # From the Phidgets documentation
246 | voltage = 0.06 * (self.controller.analog_read(self.pin) - 500.)
247 | return voltage
248 |
249 | class PhidgetsCurrent(AnalogFloatSensor):
250 | def __init__(self, *args, **kwargs):
251 | super(PhidgetsCurrent, self).__init__(*args, **kwargs)
252 |
253 | def read_value(self):
254 | # From the Phidgets documentation for the 20 amp DC sensor
255 | current = 0.05 * (self.controller.analog_read(self.pin) - 500.)
256 | return current
257 |
258 | class MaxEZ1Sensor(SonarSensor):
259 | def __init__(self, *args, **kwargs):
260 | super(MaxEZ1Sensor, self).__init__(*args, **kwargs)
261 |
262 | self.trigger_pin = kwargs['trigger_pin']
263 | self.output_pin = kwargs['output_pin']
264 |
265 | self.msg.field_of_view = 0.785398163
266 | self.msg.min_range = 0.02
267 | self.msg.max_range = 3.0
268 |
269 | def read_value(self):
270 | return self.controller.get_MaxEZ1(self.trigger_pin, self.output_pin)
271 |
272 |
273 | if __name__ == '__main__':
274 | myController = Controller()
275 | mySensor = SonarSensor(myController, "My Sonar", type=Type.PING, pin=0, rate=10)
276 |
--------------------------------------------------------------------------------
/ros_arduino_python/src/ros_arduino_python/base_controller.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | """
4 | A base controller class for the Arduino microcontroller
5 |
6 | Borrowed heavily from Mike Feguson's ArbotiX base_controller.py code.
7 |
8 | Created for the Pi Robot Project: http://www.pirobot.org
9 | Copyright (c) 2010 Patrick Goebel. All rights reserved.
10 |
11 | This program is free software; you can redistribute it and/or modify
12 | it under the terms of the GNU General Public License as published by
13 | the Free Software Foundation; either version 2 of the License, or
14 | (at your option) any later version.
15 |
16 | This program is distributed in the hope that it will be useful,
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 | GNU General Public License for more details at:
20 |
21 | http://www.gnu.org/licenses
22 | """
23 | import roslib; roslib.load_manifest('ros_arduino_python')
24 | import rospy
25 | import os
26 |
27 | from math import sin, cos, pi
28 | from geometry_msgs.msg import Quaternion, Twist, Pose
29 | from nav_msgs.msg import Odometry
30 | from tf.broadcaster import TransformBroadcaster
31 |
32 | """ Class to receive Twist commands and publish Odometry data """
33 | class BaseController:
34 | def __init__(self, arduino, base_frame, name="base_controllers"):
35 | self.arduino = arduino
36 | self.name = name
37 | self.base_frame = base_frame
38 | self.rate = float(rospy.get_param("~base_controller_rate", 10))
39 | self.timeout = rospy.get_param("~base_controller_timeout", 1.0)
40 | self.stopped = False
41 |
42 | pid_params = dict()
43 | pid_params['wheel_diameter'] = rospy.get_param("~wheel_diameter", "")
44 | pid_params['wheel_track'] = rospy.get_param("~wheel_track", "")
45 | pid_params['encoder_resolution'] = rospy.get_param("~encoder_resolution", "")
46 | pid_params['gear_reduction'] = rospy.get_param("~gear_reduction", 1.0)
47 | pid_params['Kp'] = rospy.get_param("~Kp", 20)
48 | pid_params['Kd'] = rospy.get_param("~Kd", 12)
49 | pid_params['Ki'] = rospy.get_param("~Ki", 0)
50 | pid_params['Ko'] = rospy.get_param("~Ko", 50)
51 |
52 | self.accel_limit = rospy.get_param('~accel_limit', 0.1)
53 | self.motors_reversed = rospy.get_param("~motors_reversed", False)
54 |
55 | # Set up PID parameters and check for missing values
56 | self.setup_pid(pid_params)
57 |
58 | # How many encoder ticks are there per meter?
59 | self.ticks_per_meter = self.encoder_resolution * self.gear_reduction / (self.wheel_diameter * pi)
60 |
61 | # What is the maximum acceleration we will tolerate when changing wheel speeds?
62 | self.max_accel = self.accel_limit * self.ticks_per_meter / self.rate
63 |
64 | # Track how often we get a bad encoder count (if any)
65 | self.bad_encoder_count = 0
66 |
67 | now = rospy.Time.now()
68 | self.then = now # time for determining dx/dy
69 | self.t_delta = rospy.Duration(1.0 / self.rate)
70 | self.t_next = now + self.t_delta
71 |
72 | # Internal data
73 | self.enc_left = None # encoder readings
74 | self.enc_right = None
75 | self.x = 0 # position in xy plane
76 | self.y = 0
77 | self.th = 0 # rotation in radians
78 | self.v_left = 0
79 | self.v_right = 0
80 | self.v_des_left = 0 # cmd_vel setpoint
81 | self.v_des_right = 0
82 | self.last_cmd_vel = now
83 |
84 | # Subscriptions
85 | rospy.Subscriber("cmd_vel", Twist, self.cmdVelCallback)
86 |
87 | # Clear any old odometry info
88 | self.arduino.reset_encoders()
89 |
90 | # Set up the odometry broadcaster
91 | self.odomPub = rospy.Publisher('odom', Odometry, queue_size=5)
92 | self.odomBroadcaster = TransformBroadcaster()
93 |
94 | rospy.loginfo("Started base controller for a base of " + str(self.wheel_track) + "m wide with " + str(self.encoder_resolution) + " ticks per rev")
95 | rospy.loginfo("Publishing odometry data at: " + str(self.rate) + " Hz using " + str(self.base_frame) + " as base frame")
96 |
97 | def setup_pid(self, pid_params):
98 | # Check to see if any PID parameters are missing
99 | missing_params = False
100 | for param in pid_params:
101 | if pid_params[param] == "":
102 | print("*** PID Parameter " + param + " is missing. ***")
103 | missing_params = True
104 |
105 | if missing_params:
106 | os._exit(1)
107 |
108 | self.wheel_diameter = pid_params['wheel_diameter']
109 | self.wheel_track = pid_params['wheel_track']
110 | self.encoder_resolution = pid_params['encoder_resolution']
111 | self.gear_reduction = pid_params['gear_reduction']
112 |
113 | self.Kp = pid_params['Kp']
114 | self.Kd = pid_params['Kd']
115 | self.Ki = pid_params['Ki']
116 | self.Ko = pid_params['Ko']
117 |
118 | self.arduino.update_pid(self.Kp, self.Kd, self.Ki, self.Ko)
119 |
120 | def poll(self):
121 | now = rospy.Time.now()
122 | if now > self.t_next:
123 | # Read the encoders
124 | try:
125 | left_enc, right_enc = self.arduino.get_encoder_counts()
126 | except:
127 | self.bad_encoder_count += 1
128 | rospy.logerr("Encoder exception count: " + str(self.bad_encoder_count))
129 | return
130 |
131 | dt = now - self.then
132 | self.then = now
133 | dt = dt.to_sec()
134 |
135 | # Calculate odometry
136 | if self.enc_left == None:
137 | dright = 0
138 | dleft = 0
139 | else:
140 | dright = (right_enc - self.enc_right) / self.ticks_per_meter
141 | dleft = (left_enc - self.enc_left) / self.ticks_per_meter
142 |
143 | self.enc_right = right_enc
144 | self.enc_left = left_enc
145 |
146 | dxy_ave = (dright + dleft) / 2.0
147 | dth = (dright - dleft) / self.wheel_track
148 | vxy = dxy_ave / dt
149 | vth = dth / dt
150 |
151 | if (dxy_ave != 0):
152 | dx = cos(dth) * dxy_ave
153 | dy = -sin(dth) * dxy_ave
154 | self.x += (cos(self.th) * dx - sin(self.th) * dy)
155 | self.y += (sin(self.th) * dx + cos(self.th) * dy)
156 |
157 | if (dth != 0):
158 | self.th += dth
159 |
160 | quaternion = Quaternion()
161 | quaternion.x = 0.0
162 | quaternion.y = 0.0
163 | quaternion.z = sin(self.th / 2.0)
164 | quaternion.w = cos(self.th / 2.0)
165 |
166 | # Create the odometry transform frame broadcaster.
167 | self.odomBroadcaster.sendTransform(
168 | (self.x, self.y, 0),
169 | (quaternion.x, quaternion.y, quaternion.z, quaternion.w),
170 | rospy.Time.now(),
171 | self.base_frame,
172 | "odom"
173 | )
174 |
175 | odom = Odometry()
176 | odom.header.frame_id = "odom"
177 | odom.child_frame_id = self.base_frame
178 | odom.header.stamp = now
179 | odom.pose.pose.position.x = self.x
180 | odom.pose.pose.position.y = self.y
181 | odom.pose.pose.position.z = 0
182 | odom.pose.pose.orientation = quaternion
183 | odom.twist.twist.linear.x = vxy
184 | odom.twist.twist.linear.y = 0
185 | odom.twist.twist.angular.z = vth
186 |
187 | self.odomPub.publish(odom)
188 |
189 | if now > (self.last_cmd_vel + rospy.Duration(self.timeout)):
190 | self.v_des_left = 0
191 | self.v_des_right = 0
192 |
193 | if self.v_left < self.v_des_left:
194 | self.v_left += self.max_accel
195 | if self.v_left > self.v_des_left:
196 | self.v_left = self.v_des_left
197 | else:
198 | self.v_left -= self.max_accel
199 | if self.v_left < self.v_des_left:
200 | self.v_left = self.v_des_left
201 |
202 | if self.v_right < self.v_des_right:
203 | self.v_right += self.max_accel
204 | if self.v_right > self.v_des_right:
205 | self.v_right = self.v_des_right
206 | else:
207 | self.v_right -= self.max_accel
208 | if self.v_right < self.v_des_right:
209 | self.v_right = self.v_des_right
210 |
211 | # Set motor speeds in encoder ticks per PID loop
212 | if not self.stopped:
213 | self.arduino.drive(self.v_left, self.v_right)
214 |
215 | self.t_next = now + self.t_delta
216 |
217 | def stop(self):
218 | self.stopped = True
219 | self.arduino.drive(0, 0)
220 |
221 | def cmdVelCallback(self, req):
222 | # Handle velocity-based movement requests
223 | self.last_cmd_vel = rospy.Time.now()
224 |
225 | x = req.linear.x # m/s
226 | th = req.angular.z # rad/s
227 |
228 | if x == 0:
229 | # Turn in place
230 | right = th * self.wheel_track * self.gear_reduction / 2.0
231 | left = -right
232 | elif th == 0:
233 | # Pure forward/backward motion
234 | left = right = x
235 | else:
236 | # Rotation about a point in space
237 | left = x - th * self.wheel_track * self.gear_reduction / 2.0
238 | right = x + th * self.wheel_track * self.gear_reduction / 2.0
239 |
240 | self.v_des_left = int(left * self.ticks_per_meter / self.arduino.PID_RATE)
241 | self.v_des_right = int(right * self.ticks_per_meter / self.arduino.PID_RATE)
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
--------------------------------------------------------------------------------