Thin wrapper around the pins used to control the Arduino motor shield

Dependents:   MUTTv1 ProvaMotoreSeriale SerialComLED motore2 ... more

For an example of using this library, see MUTTv1:

Import programMUTTv1

First (and very, very simple) controller program for the MUTT. See http://mbed.org/users/johnb/notebook/mutt-mbed-enabled-robot-vehicle/

Committer:
emuboy
Date:
Fri Dec 19 12:28:29 2014 +0000
Revision:
1:b6bd7c434ab5
Parent:
0:27f8679b31e5
Support for NUCLEO F401RE .

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 0:27f8679b31e5 1 /**
johnb 0:27f8679b31e5 2 @file
johnb 0:27f8679b31e5 3 @brief Class to abstract the interface to an Arduino Motor Shield R3
johnb 0:27f8679b31e5 4 See http://arduino.cc/en/Main/ArduinoMotorShieldR3
johnb 0:27f8679b31e5 5
johnb 0:27f8679b31e5 6 @author John Bailey
johnb 0:27f8679b31e5 7
johnb 0:27f8679b31e5 8 @copyright Copyright 2014 John Bailey
johnb 0:27f8679b31e5 9
johnb 0:27f8679b31e5 10 @section LICENSE
johnb 0:27f8679b31e5 11
johnb 0:27f8679b31e5 12 Licensed under the Apache License, Version 2.0 (the "License");
johnb 0:27f8679b31e5 13 you may not use this file except in compliance with the License.
johnb 0:27f8679b31e5 14 You may obtain a copy of the License at
johnb 0:27f8679b31e5 15
johnb 0:27f8679b31e5 16 http://www.apache.org/licenses/LICENSE-2.0
johnb 0:27f8679b31e5 17
johnb 0:27f8679b31e5 18 Unless required by applicable law or agreed to in writing, software
johnb 0:27f8679b31e5 19 distributed under the License is distributed on an "AS IS" BASIS,
johnb 0:27f8679b31e5 20 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 0:27f8679b31e5 21 See the License for the specific language governing permissions and
johnb 0:27f8679b31e5 22 limitations under the License.
johnb 0:27f8679b31e5 23
johnb 0:27f8679b31e5 24 */
johnb 0:27f8679b31e5 25
johnb 0:27f8679b31e5 26 #if !defined ARDUINOMOTORSHIELD_HPP
johnb 0:27f8679b31e5 27 #define ARDUINOMOTORSHIELD_HPP
johnb 0:27f8679b31e5 28
johnb 0:27f8679b31e5 29 #include "mbed.h"
johnb 0:27f8679b31e5 30 #include <stdint.h>
johnb 0:27f8679b31e5 31
johnb 0:27f8679b31e5 32 /** Class to abstract the interface to an Arduino Motor Shield R3
johnb 0:27f8679b31e5 33 See http://arduino.cc/en/Main/ArduinoMotorShieldR3
johnb 0:27f8679b31e5 34
johnb 0:27f8679b31e5 35 This class is a pretty thin wrapper around the analog inputs and
johnb 0:27f8679b31e5 36 digital outputs which interface to the motor drives
johnb 0:27f8679b31e5 37
johnb 0:27f8679b31e5 38 Currently supports:
johnb 0:27f8679b31e5 39 TARGET_KL46Z - Assumes the presence of the cross-wiring on PWM B
johnb 0:27f8679b31e5 40 (see "Issues" at http://mbed.org/users/johnb/notebook/mutt-mbed-enabled-robot-vehicle/ )
johnb 0:27f8679b31e5 41
johnb 0:27f8679b31e5 42 @todo Support for bipolar stepper motor
johnb 0:27f8679b31e5 43 @todo Support for other targets
johnb 0:27f8679b31e5 44 */
johnb 0:27f8679b31e5 45 class ArduinoMotorShield
johnb 0:27f8679b31e5 46 {
johnb 0:27f8679b31e5 47 public:
johnb 0:27f8679b31e5 48
johnb 0:27f8679b31e5 49 typedef enum
johnb 0:27f8679b31e5 50 {
johnb 0:27f8679b31e5 51 MOTOR_FORWARD = 0,
johnb 0:27f8679b31e5 52 MOTOR_BACKWARD = 1
johnb 0:27f8679b31e5 53 } MotorDirection_e;
johnb 0:27f8679b31e5 54
johnb 0:27f8679b31e5 55 /** Enumeration to distinguish between the motors connected to the shield */
johnb 0:27f8679b31e5 56 typedef enum
johnb 0:27f8679b31e5 57 {
johnb 0:27f8679b31e5 58 /* Motor 'A' */
johnb 0:27f8679b31e5 59 MOTOR_A = 0,
johnb 0:27f8679b31e5 60 /* Motor 'A' */
johnb 0:27f8679b31e5 61 MOTOR_B = 1
johnb 0:27f8679b31e5 62 } Motor_e;
johnb 0:27f8679b31e5 63
johnb 0:27f8679b31e5 64 protected:
johnb 0:27f8679b31e5 65 /** Maximum number of motors which can be connected to the shield */
johnb 0:27f8679b31e5 66 static const uint8_t MOTOR_DRIVE_COUNT = 2U;
johnb 0:27f8679b31e5 67
johnb 0:27f8679b31e5 68 /** Maximum current (per motor drive) supported by the shield */
johnb 0:27f8679b31e5 69 static const uint8_t MOTOR_MAX_CURRENT = 2U;
johnb 0:27f8679b31e5 70
johnb 0:27f8679b31e5 71 PwmOut m_a_motorControl;
johnb 0:27f8679b31e5 72 PwmOut m_b_motorControl;
johnb 0:27f8679b31e5 73 DigitalOut m_a_brake;
johnb 0:27f8679b31e5 74 DigitalOut m_b_brake;
johnb 0:27f8679b31e5 75 AnalogIn m_a_motorCurrent;
johnb 0:27f8679b31e5 76 AnalogIn m_b_motorCurrent;
johnb 0:27f8679b31e5 77 DigitalOut m_a_motorDirection;
johnb 0:27f8679b31e5 78 DigitalOut m_b_motorDirection;
johnb 0:27f8679b31e5 79
johnb 0:27f8679b31e5 80 PwmOut *m_motorControl[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 81 DigitalOut *m_brake[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 82 AnalogIn *m_motorCurrent[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 83 DigitalOut *m_motorDirection[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 84
johnb 0:27f8679b31e5 85 float m_speed[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 86 bool m_motorForward[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 87 public:
johnb 0:27f8679b31e5 88
johnb 0:27f8679b31e5 89 /** Constructor */
johnb 0:27f8679b31e5 90 ArduinoMotorShield();
johnb 0:27f8679b31e5 91
johnb 0:27f8679b31e5 92 /** Set the motor power (and direction) for the specified motor
johnb 0:27f8679b31e5 93
johnb 0:27f8679b31e5 94 \param p_motor Use this parameter to specify which motor to set the power on
johnb 0:27f8679b31e5 95 \param p_speed Set the power of the motor, ranging from -1 (full power, reverse direction) to
johnb 0:27f8679b31e5 96 +1 (full power, forward direction)
johnb 0:27f8679b31e5 97 */
johnb 0:27f8679b31e5 98 void SetMotorPower( const Motor_e p_motor, const float p_speed, bool p_force = false );
johnb 0:27f8679b31e5 99
johnb 0:27f8679b31e5 100 /** Retrieve the current being consumed by the specified motor
johnb 0:27f8679b31e5 101 \param p_motor The motor to retrieve the current for
johnb 0:27f8679b31e5 102 */
johnb 0:27f8679b31e5 103 float GetMotorCurrent( const Motor_e p_motor );
johnb 0:27f8679b31e5 104
johnb 0:27f8679b31e5 105 /** Enable or disable the brake on the specified motor. In the case that the braking
johnb 0:27f8679b31e5 106 option is enabled then (when appropriate) the shield will actively brake the motor
johnb 0:27f8679b31e5 107 rather than allowing it to coast down
johnb 0:27f8679b31e5 108
johnb 0:27f8679b31e5 109 \param p_motor The motor to set the brake option for
johnb 0:27f8679b31e5 110 \param p_enable Specify whether braking should be enabled or disabled */
johnb 0:27f8679b31e5 111 void SetBrake( const Motor_e p_motor, bool p_enable );
johnb 0:27f8679b31e5 112
johnb 0:27f8679b31e5 113 /** Set the virtual 'polarity' of a motor. This allows you to swap the meaning of "forward"
johnb 0:27f8679b31e5 114 with respect to the SetMotorPower() method. For example:
johnb 0:27f8679b31e5 115
johnb 0:27f8679b31e5 116 shield.SetMotorPolarity( MOTOR_A, MOTOR_FORWARD );
johnb 0:27f8679b31e5 117 shield.SetMotorPower( MOTOR_A, 1.0F );
johnb 0:27f8679b31e5 118 // This call will cause the motor to change direction !
johnb 0:27f8679b31e5 119 shield.SetMotorPolarity( MOTOR_A, MOTOR_BACKWARD );
johnb 0:27f8679b31e5 120 // No further change is caused by this call
johnb 0:27f8679b31e5 121 shield.SetMotorPower( MOTOR_A, 1.0F );
johnb 0:27f8679b31e5 122
johnb 0:27f8679b31e5 123 \param p_motor The motor to change the direction setting for
johnb 0:27f8679b31e5 124 \param p_dir The direction which "forward" should map to.
johnb 0:27f8679b31e5 125 */
johnb 0:27f8679b31e5 126 void SetMotorPolarity( const Motor_e p_motor, const MotorDirection_e p_dir );
johnb 0:27f8679b31e5 127 };
johnb 0:27f8679b31e5 128
johnb 0:27f8679b31e5 129 #endif // !defined ARDUINOMOTORSHIELD_HPP