Updated for the next revision of the motor board

MotorShield.cpp

Committer:
elijahsj
Date:
2020-08-26
Revision:
4:2b45973bdc67
Parent:
3:2f46953e7c8b
Child:
5:d2dffc88e94d

File content as of revision 4:2b45973bdc67:

/* Library to interface with 2.74 Motor Shield
** Uses low level HAL libraries to enable high speed PWM 
** Use as follows:
** - Create shield object and specify PWM period for the motors
** - Set the duty cycle and direction for each motor 
*/

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

MotorShield::MotorShield(int periodTicks) {
    periodTickVal = periodTicks; 
    init();
}
 
void MotorShield::init() {
    /** Initial config for the STM32H743 **/
    
    initHardware(periodTickVal);   // Setup PWM
    wait_us(100);
        
}
 
void MotorShield::motorAWrite(double duty_cycle, int direction) {
    if (direction){
            TIM12->CCR2 = int(periodTickVal * duty_cycle);
            TIM12->CCR1 = 0;
    }
    else {
            TIM12->CCR2 = 0;
            TIM12->CCR1 = int(periodTickVal * duty_cycle);
    }
    
}

void MotorShield::motorBWrite(double duty_cycle, int direction) {
    if (direction){
            TIM15->CCR2 = int(periodTickVal * duty_cycle);
            TIM15->CCR1 = 0;
    }
    else {
            TIM15->CCR2 = 0;
            TIM15->CCR1 = int(periodTickVal * duty_cycle);
    }
}

void MotorShield::motorCWrite(double duty_cycle, int direction) {
    if (direction){
            TIM13->CCR1 = int(periodTickVal * duty_cycle);
            TIM14->CCR1 = 0;
    }
    else {
            TIM13->CCR1 = 0;
            TIM14->CCR1 = int(periodTickVal * duty_cycle);
    }
}
void MotorShield::motorDWrite(double duty_cycle, int direction) {
    if (direction){
            TIM16->CCR1 = int(periodTickVal * duty_cycle);
            TIM17->CCR1 = 0;
    }
    else {
            TIM16->CCR1 = 0;
            TIM17->CCR1 = int(periodTickVal * duty_cycle);
    }
}

void MotorShield::changePeriod(int periodTicks){
    periodTickVal = periodTicks; 
    init();
}