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

Committer:
johnb
Date:
2014-02-08
Revision:
0:a3da4faf20a8
Child:
1:fac3a5bf41dd

File content as of revision 0:a3da4faf20a8:

/**
   @file
   @brief MUTT controller, version 1.
          MUTT will keep going until an obstacle is detected at which
            point it'll stop and politely wait (indefinately) for the
            obstacle to move.
            
          See http://mbed.org/users/johnb/notebook/mutt-mbed-enabled-robot-vehicle/
   
   @author John Bailey 

   @copyright Copyright 2014 John Bailey

   @section LICENSE
   
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#include "mbed.h"

/* Remove the comment to get some output on the serial line, however this
   may result in problems if the serial line's not connected (i.e. MUTT's
   not USB connected and is free roaming */
//#define SERIAL_OUTPUT

#if defined SERIAL_OUTPUT
Serial pc(USBTX, USBRX); 
#define OUTPUT_PERIOD 1000
#endif

/* Connections to the Motor Shield.  These will need to be changed for boards 
   other than the KL46Z and it also assumes the presence of the cross-wiring
   on PWM B (see "Issues" at http://mbed.org/users/johnb/notebook/mutt-mbed-enabled-robot-vehicle/ )
*/
AnalogIn irFeedback(PTC2  /* A4 */);
PwmOut pwm_b( PTA5 );
PwmOut pwm_a( PTA12 );
DigitalOut dir_a(PTD7);
DigitalOut dir_b(PTD5);

/** Determine if the IR sensor is detecting an obstacle */
bool irSensesObstacle( void )
{
    /* IR sensor is TTL, so threshold is arbitary */
    static const float ir_thresh = 0.5;
    
    /* If analogue feedback is less than the threshold then there's an obstacle */
    return( irFeedback < ir_thresh );
}
 
int main( void )
{
#if defined SERIAL_OUTPUT
    uint16_t ticker = 0;
    uint16_t next_output = 0;
#endif        

    /* Set directions on the motors.  What you need here will depend on how 
       you've wired the motors to the motor shield.  If you find that one or 
       both wheels are turning backwards then you can either change the wiring 
       or change these settings */    
    dir_a = 0;
    dir_b = 1;
    
    /* Forever is a long time ... */
    while( 1 )
    { 
        /* Determine if there's an obstacle */
        bool obstacle = irSensesObstacle();
#if defined SERIAL_OUTPUT
        if( next_output == OUTPUT_PERIOD ) 
        {
            pc.printf( "MUTT: %04u %u\r\n",ticker++,obstacle);
            next_output = 0;
        }
        next_output++;
#endif        
        
        /* Set the PWM to both motors to 1 (i.e. 100%) in the case that there's
           no obstacle, set it to 0 (i.e. 0%) in the case that ab obstacle is
           detected */ 
        pwm_a.write( !obstacle );
        pwm_b.write( !obstacle );

        /* Wait 1 millisecond before goind round the loop again */
        wait_ms( 1 );
    }
}