Motor Drive UH

Fork of ArduinoMotorShield by John Bailey

Committer:
milosm
Date:
Mon Nov 28 11:35:22 2016 +0000
Revision:
2:1dec2d3a899c
Parent:
0:27f8679b31e5
this is a test for motor drive

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:
milosm 2:1dec2d3a899c 39 TARGET_KL25Z - Assumes the presence of the cross-wiring on PWM B
johnb 0:27f8679b31e5 40 */
johnb 0:27f8679b31e5 41 class ArduinoMotorShield
johnb 0:27f8679b31e5 42 {
johnb 0:27f8679b31e5 43 public:
johnb 0:27f8679b31e5 44
johnb 0:27f8679b31e5 45 typedef enum
johnb 0:27f8679b31e5 46 {
johnb 0:27f8679b31e5 47 MOTOR_FORWARD = 0,
johnb 0:27f8679b31e5 48 MOTOR_BACKWARD = 1
johnb 0:27f8679b31e5 49 } MotorDirection_e;
johnb 0:27f8679b31e5 50
johnb 0:27f8679b31e5 51 /** Enumeration to distinguish between the motors connected to the shield */
johnb 0:27f8679b31e5 52 typedef enum
johnb 0:27f8679b31e5 53 {
johnb 0:27f8679b31e5 54 /* Motor 'A' */
johnb 0:27f8679b31e5 55 MOTOR_A = 0,
johnb 0:27f8679b31e5 56 /* Motor 'A' */
johnb 0:27f8679b31e5 57 MOTOR_B = 1
johnb 0:27f8679b31e5 58 } Motor_e;
johnb 0:27f8679b31e5 59
johnb 0:27f8679b31e5 60 protected:
johnb 0:27f8679b31e5 61 /** Maximum number of motors which can be connected to the shield */
johnb 0:27f8679b31e5 62 static const uint8_t MOTOR_DRIVE_COUNT = 2U;
johnb 0:27f8679b31e5 63
johnb 0:27f8679b31e5 64 /** Maximum current (per motor drive) supported by the shield */
johnb 0:27f8679b31e5 65 static const uint8_t MOTOR_MAX_CURRENT = 2U;
johnb 0:27f8679b31e5 66
johnb 0:27f8679b31e5 67 PwmOut m_a_motorControl;
johnb 0:27f8679b31e5 68 PwmOut m_b_motorControl;
johnb 0:27f8679b31e5 69 DigitalOut m_a_brake;
johnb 0:27f8679b31e5 70 DigitalOut m_b_brake;
johnb 0:27f8679b31e5 71 AnalogIn m_a_motorCurrent;
johnb 0:27f8679b31e5 72 AnalogIn m_b_motorCurrent;
johnb 0:27f8679b31e5 73 DigitalOut m_a_motorDirection;
johnb 0:27f8679b31e5 74 DigitalOut m_b_motorDirection;
johnb 0:27f8679b31e5 75
johnb 0:27f8679b31e5 76 PwmOut *m_motorControl[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 77 DigitalOut *m_brake[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 78 AnalogIn *m_motorCurrent[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 79 DigitalOut *m_motorDirection[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 80
johnb 0:27f8679b31e5 81 float m_speed[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 82 bool m_motorForward[ MOTOR_DRIVE_COUNT ];
johnb 0:27f8679b31e5 83 public:
johnb 0:27f8679b31e5 84
johnb 0:27f8679b31e5 85 /** Constructor */
johnb 0:27f8679b31e5 86 ArduinoMotorShield();
johnb 0:27f8679b31e5 87
johnb 0:27f8679b31e5 88 /** Set the motor power (and direction) for the specified motor
johnb 0:27f8679b31e5 89
johnb 0:27f8679b31e5 90 \param p_motor Use this parameter to specify which motor to set the power on
johnb 0:27f8679b31e5 91 \param p_speed Set the power of the motor, ranging from -1 (full power, reverse direction) to
johnb 0:27f8679b31e5 92 +1 (full power, forward direction)
johnb 0:27f8679b31e5 93 */
johnb 0:27f8679b31e5 94 void SetMotorPower( const Motor_e p_motor, const float p_speed, bool p_force = false );
johnb 0:27f8679b31e5 95
johnb 0:27f8679b31e5 96 /** Retrieve the current being consumed by the specified motor
johnb 0:27f8679b31e5 97 \param p_motor The motor to retrieve the current for
johnb 0:27f8679b31e5 98 */
johnb 0:27f8679b31e5 99 float GetMotorCurrent( const Motor_e p_motor );
johnb 0:27f8679b31e5 100
johnb 0:27f8679b31e5 101 /** Enable or disable the brake on the specified motor. In the case that the braking
johnb 0:27f8679b31e5 102 option is enabled then (when appropriate) the shield will actively brake the motor
johnb 0:27f8679b31e5 103 rather than allowing it to coast down
johnb 0:27f8679b31e5 104
johnb 0:27f8679b31e5 105 \param p_motor The motor to set the brake option for
johnb 0:27f8679b31e5 106 \param p_enable Specify whether braking should be enabled or disabled */
johnb 0:27f8679b31e5 107 void SetBrake( const Motor_e p_motor, bool p_enable );
johnb 0:27f8679b31e5 108
johnb 0:27f8679b31e5 109 /** Set the virtual 'polarity' of a motor. This allows you to swap the meaning of "forward"
johnb 0:27f8679b31e5 110 with respect to the SetMotorPower() method. For example:
johnb 0:27f8679b31e5 111
johnb 0:27f8679b31e5 112 shield.SetMotorPolarity( MOTOR_A, MOTOR_FORWARD );
johnb 0:27f8679b31e5 113 shield.SetMotorPower( MOTOR_A, 1.0F );
johnb 0:27f8679b31e5 114 // This call will cause the motor to change direction !
johnb 0:27f8679b31e5 115 shield.SetMotorPolarity( MOTOR_A, MOTOR_BACKWARD );
johnb 0:27f8679b31e5 116 // No further change is caused by this call
johnb 0:27f8679b31e5 117 shield.SetMotorPower( MOTOR_A, 1.0F );
johnb 0:27f8679b31e5 118
johnb 0:27f8679b31e5 119 \param p_motor The motor to change the direction setting for
johnb 0:27f8679b31e5 120 \param p_dir The direction which "forward" should map to.
johnb 0:27f8679b31e5 121 */
johnb 0:27f8679b31e5 122 void SetMotorPolarity( const Motor_e p_motor, const MotorDirection_e p_dir );
johnb 0:27f8679b31e5 123 };
johnb 0:27f8679b31e5 124
johnb 0:27f8679b31e5 125 #endif // !defined ARDUINOMOTORSHIELD_HPP