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

Committer:
johnb
Date:
Sat Feb 08 18:54:20 2014 +0000
Revision:
0:a3da4faf20a8
Child:
1:fac3a5bf41dd
Initial version

Who changed what in which revision?

UserRevisionLine numberNew 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 0:a3da4faf20a8 31
johnb 0:a3da4faf20a8 32 /* Remove the comment to get some output on the serial line, however this
johnb 0:a3da4faf20a8 33 may result in problems if the serial line's not connected (i.e. MUTT's
johnb 0:a3da4faf20a8 34 not USB connected and is free roaming */
johnb 0:a3da4faf20a8 35 //#define SERIAL_OUTPUT
johnb 0:a3da4faf20a8 36
johnb 0:a3da4faf20a8 37 #if defined SERIAL_OUTPUT
johnb 0:a3da4faf20a8 38 Serial pc(USBTX, USBRX);
johnb 0:a3da4faf20a8 39 #define OUTPUT_PERIOD 1000
johnb 0:a3da4faf20a8 40 #endif
johnb 0:a3da4faf20a8 41
johnb 0:a3da4faf20a8 42 /* Connections to the Motor Shield. These will need to be changed for boards
johnb 0:a3da4faf20a8 43 other than the KL46Z and it also assumes the presence of the cross-wiring
johnb 0:a3da4faf20a8 44 on PWM B (see "Issues" at http://mbed.org/users/johnb/notebook/mutt-mbed-enabled-robot-vehicle/ )
johnb 0:a3da4faf20a8 45 */
johnb 0:a3da4faf20a8 46 AnalogIn irFeedback(PTC2 /* A4 */);
johnb 0:a3da4faf20a8 47 PwmOut pwm_b( PTA5 );
johnb 0:a3da4faf20a8 48 PwmOut pwm_a( PTA12 );
johnb 0:a3da4faf20a8 49 DigitalOut dir_a(PTD7);
johnb 0:a3da4faf20a8 50 DigitalOut dir_b(PTD5);
johnb 0:a3da4faf20a8 51
johnb 0:a3da4faf20a8 52 /** Determine if the IR sensor is detecting an obstacle */
johnb 0:a3da4faf20a8 53 bool irSensesObstacle( void )
johnb 0:a3da4faf20a8 54 {
johnb 0:a3da4faf20a8 55 /* IR sensor is TTL, so threshold is arbitary */
johnb 0:a3da4faf20a8 56 static const float ir_thresh = 0.5;
johnb 0:a3da4faf20a8 57
johnb 0:a3da4faf20a8 58 /* If analogue feedback is less than the threshold then there's an obstacle */
johnb 0:a3da4faf20a8 59 return( irFeedback < ir_thresh );
johnb 0:a3da4faf20a8 60 }
johnb 0:a3da4faf20a8 61
johnb 0:a3da4faf20a8 62 int main( void )
johnb 0:a3da4faf20a8 63 {
johnb 0:a3da4faf20a8 64 #if defined SERIAL_OUTPUT
johnb 0:a3da4faf20a8 65 uint16_t ticker = 0;
johnb 0:a3da4faf20a8 66 uint16_t next_output = 0;
johnb 0:a3da4faf20a8 67 #endif
johnb 0:a3da4faf20a8 68
johnb 0:a3da4faf20a8 69 /* Set directions on the motors. What you need here will depend on how
johnb 0:a3da4faf20a8 70 you've wired the motors to the motor shield. If you find that one or
johnb 0:a3da4faf20a8 71 both wheels are turning backwards then you can either change the wiring
johnb 0:a3da4faf20a8 72 or change these settings */
johnb 0:a3da4faf20a8 73 dir_a = 0;
johnb 0:a3da4faf20a8 74 dir_b = 1;
johnb 0:a3da4faf20a8 75
johnb 0:a3da4faf20a8 76 /* Forever is a long time ... */
johnb 0:a3da4faf20a8 77 while( 1 )
johnb 0:a3da4faf20a8 78 {
johnb 0:a3da4faf20a8 79 /* Determine if there's an obstacle */
johnb 0:a3da4faf20a8 80 bool obstacle = irSensesObstacle();
johnb 0:a3da4faf20a8 81 #if defined SERIAL_OUTPUT
johnb 0:a3da4faf20a8 82 if( next_output == OUTPUT_PERIOD )
johnb 0:a3da4faf20a8 83 {
johnb 0:a3da4faf20a8 84 pc.printf( "MUTT: %04u %u\r\n",ticker++,obstacle);
johnb 0:a3da4faf20a8 85 next_output = 0;
johnb 0:a3da4faf20a8 86 }
johnb 0:a3da4faf20a8 87 next_output++;
johnb 0:a3da4faf20a8 88 #endif
johnb 0:a3da4faf20a8 89
johnb 0:a3da4faf20a8 90 /* Set the PWM to both motors to 1 (i.e. 100%) in the case that there's
johnb 0:a3da4faf20a8 91 no obstacle, set it to 0 (i.e. 0%) in the case that ab obstacle is
johnb 0:a3da4faf20a8 92 detected */
johnb 0:a3da4faf20a8 93 pwm_a.write( !obstacle );
johnb 0:a3da4faf20a8 94 pwm_b.write( !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 }