Implemented first Hangar-Service

Dependencies:   CalibrateMagneto QuaternionMath

Fork of SML2 by TobyRich GmbH

MotorDriver.cpp

Committer:
pvaibhav
Date:
2015-03-13
Revision:
12:1632d7391453
Parent:
4:e759b8c756da
Child:
18:f51b1a94a6e2

File content as of revision 12:1632d7391453:

#include "MotorDriver.h"
#include <cassert>
#include <cmath>
#define DEBUG "Motor"
#include "Logger.h"

MotorDriver::MotorDriver(I2C &i2c, const uint8_t address) : I2CPeripheral(i2c, address)
{
    INFO("Motor driver at addr=%#x ready", 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;
}