Motor Shield Example code for 2.74 Class @ MIT

Dependents:   experiment_example motor_shield_example Lab3_experiment_example jumping_leg_clicky

MotorShield.cpp

Committer:
elijahsj
Date:
2020-08-25
Revision:
1:4c3c2b7337a6
Parent:
0:f2ede00aed8a
Child:
3:2f46953e7c8b

File content as of revision 1:4c3c2b7337a6:

/* Library to interface with 2.74 Motor Shield
** Uses a modified version of FastPWM library to improve PWM accuracy 
*/

#include "mbed.h"
#include "MotorShield.h"
#include "HardwareSetup.h"

//GPIOStruct gpio;

MotorShield::MotorShield(PinName forwardPin, PinName reversePin) {

    init();
}
 
void MotorShield::init() {
    /** Initial config for the STM32H743 **/
    
    //Init_All_HW(&gpio);                                                         // Setup PWM, ADC, GPIO
    //wait_us(100);
    
    //TIM12->CCR2 = (PWM_ARR>>1)*(0.5f);
    //TIM12->CCR1 = (PWM_ARR>>1)*(0.5f);
        
    direction_val = 0;
    duty_cycle_val = 0;
    period_val = 10.0; 
    
}
 
void MotorShield::write(double duty_cycle) {
    duty_cycle_val = duty_cycle;
    writePWM();
}

void MotorShield::period(double period) {
    period_val = period;
    writePWM();
}

void MotorShield::direction(int direction) {
    direction_val = direction;
    writePWM();
}

void MotorShield::writePWM(){
    if (direction_val == 0){
        //forward.period_us(period_val);
        //forward.write(duty_cycle_val);
        //reverse.period_us(period_val);
        //reverse.write(0);
    }
    else{
        //reverse.period_us(period_val);
        //reverse.write(duty_cycle_val);
        //forward.period_us(period_val);
        //forward.write(0);
    }
}