md: C implementation; C++ wraps C

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2016-10-18 17:02:33 -06:00
parent df5b3805c5
commit 8ac8be9e0a
16 changed files with 813 additions and 397 deletions

View File

@@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
* Copyright (c) 2014-2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -23,234 +23,165 @@
*/
#pragma once
#include <stdint.h>
#include <sys/time.h>
#include <string>
#include <mraa/types.hpp>
#include <mraa/i2c.hpp>
#define MD_I2C_BUS 0
#define MD_DEFAULT_I2C_ADDR 0x0f
#include <md.h>
namespace upm {
/**
* @brief I2C Motor Driver library
* @defgroup md libupm-md
* @ingroup seeed i2c motor robok
*/
/**
* @library md
* @sensor md
* @comname I2C Motor Driver
* @altname Grove Motor Driver
* @type motor
* @man seeed
* @con i2c
* @kit robok
*
* @brief API for the I2C Motor Driver
*
* This class implements support for the I2C Motor Driver.
* This device can support a single 4-wire stepper motor, or two
* 2-wire DC motors. The device contains an Atmel* ATmega8L
* microcontroller that manages an L298N H-bridge driver chip.
*
* This device supports an I2C bus speed of 100Khz only.
*
* The module does not provide any telemetry or status - it only
* accepts I2C commands for its various operations.
*
* This module was tested with version 1.3 of the I2C Motor
* Driver.
*
* For stepper operation, this driver can run in one of two modes -
* Mode 1, where this driver handles the stepping operation, and
* Mode 2, where this driver simply sends commands to the
* Motor Driver, and it handles the stepping operation. Mode2
* requires updated (and working) firmware to be loaded onto the
* device.
*
* The default stepper operation mode is Mode1, which is generally
* more flexible and is supported on all firmware revisions.
*
* @image html md.jpg
* An example showing the use of a DC motor
* @snippet md.cxx Interesting
* An example showing the use of a 4-wire stepper
* @snippet md-stepper.cxx Interesting
*/
class MD {
public:
// MD registers
typedef enum { SET_SPEED = 0x82,
SET_PWM_FREQ = 0x84,
SET_DIRECTION = 0xaa,
SET_MOTOR_A = 0xa1, // not documented
SET_MOTOR_B = 0xa5, // not documented
STEPPER_ENABLE = 0x1a,
STEPPER_DISABLE = 0x1b,
STEPPER_NUM_STEPS = 0x1c
} REG_T;
// legal directions for the stepper
typedef enum { STEP_DIR_CCW = 0x01,
STEP_DIR_CW = 0x00
} STEP_DIRECTION_T;
// legal directions for individual DC motors
typedef enum { DIR_CCW = 0x02,
DIR_CW = 0x01
} DC_DIRECTION_T;
// stepper modes
typedef enum { STEP_MODE1 = 0x00,
STEP_MODE2 = 0x01
} STEP_MODE_T;
/**
* MD constructor
*
* @param bus I2C bus to use
* @param address I2C address to use
* @brief I2C Motor Driver library
* @defgroup md libupm-md
* @ingroup seeed i2c motor robok
*/
MD(int bus=MD_I2C_BUS,
uint8_t address=MD_DEFAULT_I2C_ADDR);
/**
* MD destructor
*/
~MD();
/**
* Composes and writes a 3-byte packet to the controller
* @library md
* @sensor md
* @comname I2C Motor Driver
* @altname Grove Motor Driver
* @type motor
* @man seeed
* @con i2c
* @kit robok
*
* @param reg Register location
* @param data1 First byte of data
* @param data2 Second byte of data
* @return True if successful
*/
bool writePacket(REG_T reg, uint8_t data1, uint8_t data2);
/**
* To control DC motors, sets the speed of motors A & B.
* Valid values are 0-255.
* @brief API for the I2C Motor Driver
*
* @param speedA Speed of motor A
* @param speedB Speed of motor B
* @return True if successful
*/
bool setMotorSpeeds(uint8_t speedA, uint8_t speedB);
/**
* To control DC motors, sets the PWM frequency prescale
* factor. Note: this register is not ducumented other than to say
* the default value is 0x03. Presumably, this is the timer
* prescale factor used on the ATMega MCU timer driving the PWM.
* This class implements support for the I2C Motor Driver.
* This device can support a single 4-wire stepper motor, or two
* 2-wire DC motors. The device contains an Atmel* ATmega8L
* microcontroller that manages an L298N H-bridge driver chip.
*
* @param freq PWM prescale frequency; default is 0x03
* @return True if successful
*/
bool setPWMFrequencyPrescale(uint8_t freq=0x03);
/**
* To control DC motors, sets the directions of motors A & B
* This device supports an I2C bus speed of 100Khz only.
*
* @param dirA Direction for motor A, DIR_CW or DIR_CCW
* @param dirB Direction for motor B, DIR_CW or DIR_CCW
* @return True if successful
*/
bool setMotorDirections(DC_DIRECTION_T dirA, DC_DIRECTION_T dirB);
/**
* To control a stepper motor, sets its direction and speed, and
* then starts operation. For Mode2, this method will return
* immediately. For Mode1 (the default) this method returns when
* the number of steps specified by setStepperSteps() has
* completed.
* The module does not provide any telemetry or status - it only
* accepts I2C commands for its various operations.
*
* @param dir Direction, STEP_DIR_CW or STEP_DIR_CCW
* @param speed Motor speed. Valid range is 1-255. For Mode 1
* (default), this specifies the speed in RPM's. For Mode 2,
* speed is multiplied by 4ms by the board, so higher numbers
* will mean a slower speed.
* @return True if successful
*/
bool enableStepper(STEP_DIRECTION_T dir, uint8_t speed);
/**
* To control a stepper motor, stops the stepper motor.
* This module was tested with version 1.3 of the I2C Motor
* Driver.
*
* @return True if successful
*/
bool disableStepper();
/**
* To control a stepper motor, specifies the number of steps to
* execute. For Mode2, valid values are between 1-255, 255 means
* continuous rotation.
* For stepper operation, this driver can run in one of two modes -
* Mode 1, where this driver handles the stepping operation, and
* Mode 2, where this driver simply sends commands to the
* Motor Driver, and it handles the stepping operation. Mode2
* requires updated (and working) firmware to be loaded onto the
* device.
*
* For Mode1 (the default) steps can be any positive integer.
* The default stepper operation mode is Mode1, which is generally
* more flexible and is supported on all firmware revisions.
*
* @param steps Number of steps to execute. 255 (only in Mode2)
* means continuous rotation.
* @return True if successful
* @image html md.jpg
* An example showing the use of a DC motor
* @snippet md.cxx Interesting
* An example showing the use of a 4-wire stepper
* @snippet md-stepper.cxx Interesting
*/
bool setStepperSteps(unsigned int steps);
class MD {
/**
* Configure the initial Stepper parameters. This should be
* called before any other stepper method.
*
* @param stepsPerRev The number of steps required to complete one
* full revolution.
* @param mode The stepper operating mode, default STEP_MODE1
* @return Elapsed milliseconds
*/
void configStepper(unsigned int stepsPerRev, STEP_MODE_T mode=STEP_MODE1);
public:
/**
* MD constructor
*
* @param bus I2C bus to use
* @param address I2C address to use
*/
MD(int bus=MD_I2C_BUS, uint8_t address=MD_DEFAULT_I2C_ADDR);
protected:
mraa::I2c m_i2c;
uint8_t m_addr;
/**
* MD destructor
*/
~MD();
private:
// steps per revolution
int m_stepsPerRev;
int m_currentStep;
uint32_t m_stepDelay;
uint32_t m_totalSteps;
STEP_MODE_T m_stepMode;
/**
* Composes and writes a 3-byte packet to the controller
*
* @param reg Register location
* @param data1 First byte of data
* @param data2 Second byte of data
* @return True if successful
*/
bool writePacket(MD_REG_T reg, uint8_t data1, uint8_t data2);
/**
* Steps the motor one tick
*
*/
void stepperStep();
/**
* To control DC motors, sets the speed of motors A & B.
* Valid values are 0-255.
*
* @param speedA Speed of motor A
* @param speedB Speed of motor B
* @return True if successful
*/
bool setMotorSpeeds(uint8_t speedA, uint8_t speedB);
// step direction: - 1 = forward, -1 = backward
int m_stepDirection;
/**
* To control DC motors, sets the PWM frequency prescale
* factor. Note: this register is not ducumented other than to say
* the default value is 0x03. Presumably, this is the timer
* prescale factor used on the ATMega MCU timer driving the PWM.
*
* @param freq PWM prescale frequency; default is 0x03
* @return True if successful
*/
bool setPWMFrequencyPrescale(uint8_t freq=0x03);
// This is a NOOP value used to pad packets
static const uint8_t MD_NOOP = 0x01;
// our timer
struct timeval m_startTime;
/**
* To control DC motors, sets the directions of motors A & B
*
* @param dirA Direction for motor A, DIR_CW or DIR_CCW
* @param dirB Direction for motor B, DIR_CW or DIR_CCW
* @return True if successful
*/
bool setMotorDirections(MD_DC_DIRECTION_T dirA, MD_DC_DIRECTION_T dirB);
/**
* Returns the number of milliseconds elapsed since initClock()
* was last called.
*
* @return Elapsed milliseconds
*/
uint32_t getMillis();
/**
* To control a stepper motor, sets its direction and speed, and
* then starts operation. For Mode2, this method will return
* immediately. For Mode1 (the default) this method returns when
* the number of steps specified by setStepperSteps() has
* completed.
*
* @param dir Direction, STEP_DIR_CW or STEP_DIR_CCW
* @param speed Motor speed. Valid range is 1-255. For Mode 1
* (default), this specifies the speed in RPM's. For Mode 2,
* speed is multiplied by 4ms by the board, so higher numbers
* will mean a slower speed.
* @return True if successful
*/
bool enableStepper(MD_STEP_DIRECTION_T dir, uint8_t speed);
/**
* Resets the clock
*
*/
void initClock();
/**
* To control a stepper motor, stops the stepper motor.
*
* @return True if successful
*/
bool disableStepper();
};
/**
* To control a stepper motor, specifies the number of steps to
* execute. For Mode2, valid values are between 1-255, 255 means
* continuous rotation.
*
* For Mode1 (the default) steps can be any positive integer.
*
* @param steps Number of steps to execute. 255 (only in Mode2)
* means continuous rotation.
* @return True if successful
*/
bool setStepperSteps(unsigned int steps);
/**
* Configure the initial Stepper parameters. This should be
* called before any other stepper method.
*
* @param stepsPerRev The number of steps required to complete one
* full revolution.
* @param mode The stepper operating mode, default STEP_MODE1
* @return Elapsed milliseconds
*/
void configStepper(unsigned int stepsPerRev,
MD_STEP_MODE_T mode=MD_STEP_MODE1);
protected:
md_context m_md;
};
}