Andrea Campanella / ArduinoMotorShield

Fork of ArduinoMotorShield by John Bailey

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ArduinoMotorShield.hpp Source File

ArduinoMotorShield.hpp

Go to the documentation of this file.
00001 /**
00002    @file
00003    @brief Class to abstract the interface to an Arduino Motor Shield R3
00004           See http://arduino.cc/en/Main/ArduinoMotorShieldR3
00005    
00006    @author John Bailey 
00007 
00008    @copyright Copyright 2014 John Bailey
00009 
00010    @section LICENSE
00011    
00012 Licensed under the Apache License, Version 2.0 (the "License");
00013 you may not use this file except in compliance with the License.
00014 You may obtain a copy of the License at
00015 
00016     http://www.apache.org/licenses/LICENSE-2.0
00017 
00018 Unless required by applicable law or agreed to in writing, software
00019 distributed under the License is distributed on an "AS IS" BASIS,
00020 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021 See the License for the specific language governing permissions and
00022 limitations under the License.
00023 
00024 */
00025 
00026 #if !defined ARDUINOMOTORSHIELD_HPP
00027 #define      ARDUINOMOTORSHIELD_HPP
00028 
00029 #include "mbed.h"
00030 #include <stdint.h>
00031 
00032 /** Class to abstract the interface to an Arduino Motor Shield R3
00033     See http://arduino.cc/en/Main/ArduinoMotorShieldR3
00034     
00035     This class is a pretty thin wrapper around the analog inputs and 
00036     digital outputs which interface to the motor drives
00037     
00038     Currently supports:
00039         TARGET_KL46Z - Assumes the presence of the cross-wiring on PWM B 
00040                        (see "Issues" at http://mbed.org/users/johnb/notebook/mutt-mbed-enabled-robot-vehicle/ )
00041     
00042     @todo Support for bipolar stepper motor
00043     @todo Support for other targets
00044 */
00045 class ArduinoMotorShield
00046 {    
00047     public:
00048     
00049         typedef enum
00050         {
00051             MOTOR_FORWARD  = 0,
00052             MOTOR_BACKWARD = 1
00053         } MotorDirection_e;
00054     
00055         /** Enumeration to distinguish between the motors connected to the shield */    
00056         typedef enum
00057         {
00058             /* Motor 'A' */
00059             MOTOR_A = 0,
00060             /* Motor 'A' */
00061             MOTOR_B = 1
00062         } Motor_e;
00063     
00064     protected:
00065         /** Maximum number of motors which can be connected to the shield */
00066         static const uint8_t MOTOR_DRIVE_COUNT = 2U;
00067         
00068         /** Maximum current (per motor drive) supported by the shield */
00069         static const uint8_t MOTOR_MAX_CURRENT = 2U;
00070     
00071         PwmOut     m_a_motorControl;
00072         PwmOut     m_b_motorControl;
00073         DigitalOut m_a_brake;
00074         DigitalOut m_b_brake;
00075         AnalogIn   m_a_motorCurrent;
00076         AnalogIn   m_b_motorCurrent;
00077         DigitalOut m_a_motorDirection;    
00078         DigitalOut m_b_motorDirection;    
00079     
00080         PwmOut     *m_motorControl[ MOTOR_DRIVE_COUNT ];
00081         DigitalOut *m_brake[ MOTOR_DRIVE_COUNT ];
00082         AnalogIn   *m_motorCurrent[ MOTOR_DRIVE_COUNT ];
00083         DigitalOut *m_motorDirection[ MOTOR_DRIVE_COUNT ];
00084         
00085         float      m_speed[ MOTOR_DRIVE_COUNT ];
00086         bool       m_motorForward[ MOTOR_DRIVE_COUNT ];
00087     public:
00088     
00089         /** Constructor */
00090         ArduinoMotorShield();
00091         
00092         /** Set the motor power (and direction) for the specified motor
00093          
00094             \param p_motor Use this parameter to specify which motor to set the power on
00095             \param p_speed Set the power of the motor, ranging from -1 (full power, reverse direction) to
00096                            +1 (full power, forward direction)
00097         */
00098         void SetMotorPower( const Motor_e p_motor, const float p_speed, bool p_force = false );
00099         
00100         /** Retrieve the current being consumed by the specified motor
00101             \param p_motor The motor to retrieve the current for
00102         */
00103         float GetMotorCurrent( const Motor_e p_motor );
00104         
00105         /** Enable or disable the brake on the specified motor.  In the case that the braking
00106             option is enabled then (when appropriate) the shield will actively brake the motor
00107             rather than allowing it to coast down
00108             
00109             \param p_motor The motor to set the brake option for
00110             \param p_enable Specify whether braking should be enabled or disabled */
00111         void SetBrake( const Motor_e p_motor, bool p_enable );
00112         
00113         /** Set the virtual 'polarity' of a motor.  This allows you to swap the meaning of "forward"
00114             with respect to the SetMotorPower() method.  For example:
00115             
00116                 shield.SetMotorPolarity( MOTOR_A, MOTOR_FORWARD );
00117                 shield.SetMotorPower( MOTOR_A, 1.0F );
00118                 // This call will cause the motor to change direction !
00119                 shield.SetMotorPolarity( MOTOR_A, MOTOR_BACKWARD );
00120                 // No further change is caused by this call
00121                 shield.SetMotorPower( MOTOR_A, 1.0F );
00122                 
00123             \param p_motor The motor to change the direction setting for
00124             \param p_dir   The direction which "forward" should map to.
00125         */
00126         void SetMotorPolarity( const Motor_e p_motor, const MotorDirection_e p_dir );
00127 };
00128 
00129 #endif // !defined ARDUINOMOTORSHIELD_HPP