Implemented first Hangar-Service

Dependencies:   CalibrateMagneto QuaternionMath

Fork of SML2 by TobyRich GmbH

MotorDriver.cpp

Committer:
pvaibhav
Date:
2015-01-21
Revision:
3:ee90a9ada112
Parent:
0:943820483318
Child:
4:e759b8c756da

File content as of revision 3:ee90a9ada112:

#include "MotorDriver.h"
#include <cassert>
#include <cmath>

MotorDriver::MotorDriver(I2C &i2c, const uint8_t address) : I2CPeripheral(i2c, address) {}

void MotorDriver::setVoltage(const float voltage) {
    assert(abs(voltage) <= 5.06);
    
    const bool stop = fabs(voltage) < 0.48;
    const float Vr = 1.285; // internal reference voltage
    
    if (stop) {
        write_reg(0x00, 0x03); // last 2 bits = 11 = "brake"
    } else {
        const uint8_t DAC_val = ceil( (16.0 * fabs(voltage) / Vr) - 1.0 ); // derived from table on DRV8830 datasheet page 10
        const uint8_t direction = (voltage > 0.0) ? 0x01 : 0x02; // forward or reverse is set through last 2 bits
        write_reg(0x00, (DAC_val << 2) | direction);
    }
}

MotorDriver& MotorDriver::operator=(const float voltage) {
    setVoltage(voltage);
    return *this;
}