![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
First (and very, very simple) controller program for the MUTT. See http://mbed.org/users/johnb/notebook/mutt-mbed-enabled-robot-vehicle/
Dependencies: mbed ArduinoMotorShield
main.cpp@1:fac3a5bf41dd, 2014-02-12 (annotated)
- Committer:
- johnb
- Date:
- Wed Feb 12 23:04:33 2014 +0000
- Revision:
- 1:fac3a5bf41dd
- Parent:
- 0:a3da4faf20a8
Update to use ArduinoMotorShield library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnb | 0:a3da4faf20a8 | 1 | /** |
johnb | 0:a3da4faf20a8 | 2 | @file |
johnb | 0:a3da4faf20a8 | 3 | @brief MUTT controller, version 1. |
johnb | 0:a3da4faf20a8 | 4 | MUTT will keep going until an obstacle is detected at which |
johnb | 0:a3da4faf20a8 | 5 | point it'll stop and politely wait (indefinately) for the |
johnb | 0:a3da4faf20a8 | 6 | obstacle to move. |
johnb | 0:a3da4faf20a8 | 7 | |
johnb | 0:a3da4faf20a8 | 8 | See http://mbed.org/users/johnb/notebook/mutt-mbed-enabled-robot-vehicle/ |
johnb | 0:a3da4faf20a8 | 9 | |
johnb | 0:a3da4faf20a8 | 10 | @author John Bailey |
johnb | 0:a3da4faf20a8 | 11 | |
johnb | 0:a3da4faf20a8 | 12 | @copyright Copyright 2014 John Bailey |
johnb | 0:a3da4faf20a8 | 13 | |
johnb | 0:a3da4faf20a8 | 14 | @section LICENSE |
johnb | 0:a3da4faf20a8 | 15 | |
johnb | 0:a3da4faf20a8 | 16 | Licensed under the Apache License, Version 2.0 (the "License"); |
johnb | 0:a3da4faf20a8 | 17 | you may not use this file except in compliance with the License. |
johnb | 0:a3da4faf20a8 | 18 | You may obtain a copy of the License at |
johnb | 0:a3da4faf20a8 | 19 | |
johnb | 0:a3da4faf20a8 | 20 | http://www.apache.org/licenses/LICENSE-2.0 |
johnb | 0:a3da4faf20a8 | 21 | |
johnb | 0:a3da4faf20a8 | 22 | Unless required by applicable law or agreed to in writing, software |
johnb | 0:a3da4faf20a8 | 23 | distributed under the License is distributed on an "AS IS" BASIS, |
johnb | 0:a3da4faf20a8 | 24 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnb | 0:a3da4faf20a8 | 25 | See the License for the specific language governing permissions and |
johnb | 0:a3da4faf20a8 | 26 | limitations under the License. |
johnb | 0:a3da4faf20a8 | 27 | |
johnb | 0:a3da4faf20a8 | 28 | */ |
johnb | 0:a3da4faf20a8 | 29 | |
johnb | 0:a3da4faf20a8 | 30 | #include "mbed.h" |
johnb | 1:fac3a5bf41dd | 31 | #include "ArduinoMotorShield.hpp" |
johnb | 0:a3da4faf20a8 | 32 | |
johnb | 1:fac3a5bf41dd | 33 | ArduinoMotorShield shield; |
johnb | 0:a3da4faf20a8 | 34 | AnalogIn irFeedback(PTC2 /* A4 */); |
johnb | 0:a3da4faf20a8 | 35 | |
johnb | 0:a3da4faf20a8 | 36 | /** Determine if the IR sensor is detecting an obstacle */ |
johnb | 0:a3da4faf20a8 | 37 | bool irSensesObstacle( void ) |
johnb | 0:a3da4faf20a8 | 38 | { |
johnb | 0:a3da4faf20a8 | 39 | /* IR sensor is TTL, so threshold is arbitary */ |
johnb | 0:a3da4faf20a8 | 40 | static const float ir_thresh = 0.5; |
johnb | 0:a3da4faf20a8 | 41 | |
johnb | 0:a3da4faf20a8 | 42 | /* If analogue feedback is less than the threshold then there's an obstacle */ |
johnb | 0:a3da4faf20a8 | 43 | return( irFeedback < ir_thresh ); |
johnb | 0:a3da4faf20a8 | 44 | } |
johnb | 1:fac3a5bf41dd | 45 | |
johnb | 1:fac3a5bf41dd | 46 | /* Remove the comment to get some output on the serial line, however this |
johnb | 1:fac3a5bf41dd | 47 | may result in problems if the serial line's not connected (i.e. MUTT's |
johnb | 1:fac3a5bf41dd | 48 | not USB connected and is free roaming) */ |
johnb | 1:fac3a5bf41dd | 49 | #define SERIAL_OUTPUT |
johnb | 1:fac3a5bf41dd | 50 | |
johnb | 1:fac3a5bf41dd | 51 | #if defined SERIAL_OUTPUT |
johnb | 1:fac3a5bf41dd | 52 | #define OUTPUT_PERIOD 1000 |
johnb | 1:fac3a5bf41dd | 53 | Serial pc(USBTX, USBRX); |
johnb | 1:fac3a5bf41dd | 54 | uint16_t ticker = 0; |
johnb | 1:fac3a5bf41dd | 55 | uint16_t next_output = 0; |
johnb | 1:fac3a5bf41dd | 56 | |
johnb | 1:fac3a5bf41dd | 57 | void serial_status( void ) |
johnb | 1:fac3a5bf41dd | 58 | { |
johnb | 1:fac3a5bf41dd | 59 | if( next_output == OUTPUT_PERIOD ) |
johnb | 1:fac3a5bf41dd | 60 | { |
johnb | 1:fac3a5bf41dd | 61 | pc.printf( "MUTT: %04u Obstacle:%u Current A: %0.2f Current B: %0.2f\r\n", |
johnb | 1:fac3a5bf41dd | 62 | ticker++,irSensesObstacle(), |
johnb | 1:fac3a5bf41dd | 63 | shield.GetMotorCurrent(ArduinoMotorShield::MOTOR_A), |
johnb | 1:fac3a5bf41dd | 64 | shield.GetMotorCurrent(ArduinoMotorShield::MOTOR_B) ); |
johnb | 1:fac3a5bf41dd | 65 | next_output = 0; |
johnb | 1:fac3a5bf41dd | 66 | } |
johnb | 1:fac3a5bf41dd | 67 | next_output++; |
johnb | 1:fac3a5bf41dd | 68 | } |
johnb | 1:fac3a5bf41dd | 69 | #endif |
johnb | 1:fac3a5bf41dd | 70 | |
johnb | 0:a3da4faf20a8 | 71 | int main( void ) |
johnb | 0:a3da4faf20a8 | 72 | { |
johnb | 0:a3da4faf20a8 | 73 | /* Set directions on the motors. What you need here will depend on how |
johnb | 0:a3da4faf20a8 | 74 | you've wired the motors to the motor shield. If you find that one or |
johnb | 0:a3da4faf20a8 | 75 | both wheels are turning backwards then you can either change the wiring |
johnb | 0:a3da4faf20a8 | 76 | or change these settings */ |
johnb | 1:fac3a5bf41dd | 77 | shield.SetMotorPolarity( ArduinoMotorShield::MOTOR_A, ArduinoMotorShield::MOTOR_FORWARD ); |
johnb | 1:fac3a5bf41dd | 78 | shield.SetMotorPolarity( ArduinoMotorShield::MOTOR_B, ArduinoMotorShield::MOTOR_BACKWARD ); |
johnb | 0:a3da4faf20a8 | 79 | |
johnb | 0:a3da4faf20a8 | 80 | /* Forever is a long time ... */ |
johnb | 0:a3da4faf20a8 | 81 | while( 1 ) |
johnb | 0:a3da4faf20a8 | 82 | { |
johnb | 0:a3da4faf20a8 | 83 | /* Determine if there's an obstacle */ |
johnb | 0:a3da4faf20a8 | 84 | bool obstacle = irSensesObstacle(); |
johnb | 1:fac3a5bf41dd | 85 | |
johnb | 0:a3da4faf20a8 | 86 | #if defined SERIAL_OUTPUT |
johnb | 1:fac3a5bf41dd | 87 | serial_status(); |
johnb | 0:a3da4faf20a8 | 88 | #endif |
johnb | 0:a3da4faf20a8 | 89 | |
johnb | 1:fac3a5bf41dd | 90 | /* Set the both motors to 1 (i.e. full power) in the case that there's |
johnb | 1:fac3a5bf41dd | 91 | no obstacle, set it to 0 (i.e. no power) in the case that an obstacle is |
johnb | 0:a3da4faf20a8 | 92 | detected */ |
johnb | 1:fac3a5bf41dd | 93 | shield.SetMotorPower( ArduinoMotorShield::MOTOR_A, !obstacle ); |
johnb | 1:fac3a5bf41dd | 94 | shield.SetMotorPower( ArduinoMotorShield::MOTOR_B, !obstacle ); |
johnb | 0:a3da4faf20a8 | 95 | |
johnb | 0:a3da4faf20a8 | 96 | /* Wait 1 millisecond before goind round the loop again */ |
johnb | 0:a3da4faf20a8 | 97 | wait_ms( 1 ); |
johnb | 0:a3da4faf20a8 | 98 | } |
johnb | 0:a3da4faf20a8 | 99 | } |