2015-01-23 16:30:22 -07:00
|
|
|
/*
|
|
|
|
* Author: Jon Trulson <jtrulson@ics.com>
|
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
2015-09-10 11:09:54 -06:00
|
|
|
#include <stdexcept>
|
2015-01-23 16:30:22 -07:00
|
|
|
|
|
|
|
#include "grovemd.h"
|
|
|
|
|
|
|
|
using namespace upm;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
GroveMD::GroveMD(int bus, uint8_t address)
|
|
|
|
{
|
|
|
|
m_addr = address;
|
|
|
|
|
|
|
|
// setup our i2c link
|
|
|
|
if ( !(m_i2c = mraa_i2c_init(bus)) )
|
|
|
|
{
|
2015-09-10 11:09:54 -06:00
|
|
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
|
|
|
": mraa_i2c_init() failed");
|
2015-01-23 16:30:22 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this board *requires* 100Khz i2c bus only
|
|
|
|
mraa_result_t rv;
|
2015-09-16 10:18:38 -07:00
|
|
|
if ( (rv = mraa_i2c_frequency(m_i2c, MRAA_I2C_STD)) != MRAA_SUCCESS )
|
|
|
|
{
|
2015-09-10 11:09:54 -06:00
|
|
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
|
|
|
": mraa_i2c_frequency(MRAA_I2C_STD) failed");
|
2015-09-16 10:18:38 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-01-23 16:30:22 -07:00
|
|
|
|
|
|
|
if (mraa_i2c_address(m_i2c, m_addr))
|
|
|
|
{
|
2015-09-10 11:09:54 -06:00
|
|
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
|
|
|
": mraa_i2c_address() failed");
|
2015-01-23 16:30:22 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GroveMD::~GroveMD()
|
|
|
|
{
|
|
|
|
setMotorSpeeds(0, 0);
|
|
|
|
mraa_i2c_stop(m_i2c);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroveMD::writePacket(REG_T reg, uint8_t data1, uint8_t data2)
|
|
|
|
{
|
|
|
|
uint8_t buf[3];
|
|
|
|
|
|
|
|
buf[0] = reg;
|
|
|
|
buf[1] = data1;
|
|
|
|
buf[2] = data2;
|
|
|
|
|
|
|
|
mraa_result_t rv;
|
|
|
|
if ( (rv = mraa_i2c_address(m_i2c, m_addr)) != MRAA_SUCCESS )
|
|
|
|
{
|
2015-09-10 11:09:54 -06:00
|
|
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
|
|
|
": mraa_i2c_address() failed");
|
2015-01-23 16:30:22 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This sleep appears to be required. Without it, writes randomly
|
|
|
|
// fail (no ACK received). This happens most often on the SET_SPEED
|
|
|
|
// packet. I am guessing that there is a timing problem and/or bug
|
|
|
|
// in the motor driver's firmware.
|
|
|
|
|
|
|
|
usleep(100);
|
|
|
|
|
|
|
|
if ( (rv = mraa_i2c_write(m_i2c, buf, 3)) != MRAA_SUCCESS )
|
|
|
|
{
|
2015-09-10 11:09:54 -06:00
|
|
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
|
|
|
": mraa_i2c_write() failed");
|
2015-01-23 16:30:22 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroveMD::setMotorSpeeds(uint8_t speedA, uint8_t speedB)
|
|
|
|
{
|
|
|
|
return writePacket(SET_SPEED, speedA, speedB);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroveMD::setPWMFrequencyPrescale(uint8_t freq)
|
|
|
|
{
|
|
|
|
return writePacket(SET_PWM_FREQ, freq, GROVEMD_NOOP);
|
|
|
|
}
|
|
|
|
|
2015-03-18 12:20:19 -06:00
|
|
|
bool GroveMD::setMotorDirections(DC_DIRECTION_T dirA, DC_DIRECTION_T dirB)
|
2015-01-23 16:30:22 -07:00
|
|
|
{
|
2015-03-18 12:20:19 -06:00
|
|
|
uint8_t dir = ((dirB & 0x03) << 2) | (dirA & 0x03);
|
2015-01-23 16:30:22 -07:00
|
|
|
return writePacket(SET_DIRECTION, dir, GROVEMD_NOOP);
|
|
|
|
}
|
|
|
|
|
2015-03-18 12:20:19 -06:00
|
|
|
bool GroveMD::enableStepper(STEP_DIRECTION_T dir, uint8_t speed)
|
2015-01-23 16:30:22 -07:00
|
|
|
{
|
|
|
|
return writePacket(STEPPER_ENABLE, dir, speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroveMD::disableStepper()
|
|
|
|
{
|
|
|
|
return writePacket(STEPPER_DISABLE, GROVEMD_NOOP, GROVEMD_NOOP);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroveMD::setStepperSteps(uint8_t steps)
|
|
|
|
{
|
|
|
|
if (steps == 0)
|
|
|
|
{
|
|
|
|
// invalid
|
2015-09-10 11:09:54 -06:00
|
|
|
throw std::out_of_range(std::string(__FUNCTION__) +
|
|
|
|
": invalid number of steps. " +
|
|
|
|
"Valid values are between 1 and 255.");
|
2015-01-23 16:30:22 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return writePacket(STEPPER_NUM_STEPS, steps, GROVEMD_NOOP);
|
|
|
|
}
|
|
|
|
|