upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Data Structures | Functions | Typedefs
Include dependency graph for md.h:

API Description

/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 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 <stdio.h>
#include <md.h>
#include <upm_utilities.h>
int main(int argc, char **argv)
{
// Instantiate an I2C Motor Driver on I2C bus 0
md_context motors = md_init(MD_I2C_BUS, MD_DEFAULT_I2C_ADDR);
if (!motors)
{
printf("md_init() failed\n");
return 1;
}
// set direction to CW and set speed to 50%
printf("Spin M1 and M2 at half speed for 3 seconds\n");
md_set_motor_directions(motors, MD_DIR_CW, MD_DIR_CW);
md_set_motor_speeds(motors, 127, 127);
upm_delay(3);
// counter clockwise
printf("Reversing M1 and M2 for 3 seconds\n");
md_set_motor_directions(motors, MD_DIR_CCW, MD_DIR_CCW);
upm_delay(3);
printf("Stopping motors\n");
md_set_motor_speeds(motors, 0, 0);
printf("Exiting...\n");
md_close(motors);
return 0;
}

Go to the source code of this file.

Data Structures

struct  _md_context
 

Functions

md_context md_init (int bus, uint8_t address)
 
void md_close (md_context dev)
 
bool md_write_packet (const md_context dev, MD_REG_T reg, uint8_t data1, uint8_t data2)
 
bool md_set_motor_speeds (const md_context dev, uint8_t speedA, uint8_t speedB)
 
bool md_set_pwm_frequency_prescale (const md_context dev, uint8_t freq)
 
bool md_set_motor_directions (const md_context dev, MD_DC_DIRECTION_T dirA, MD_DC_DIRECTION_T dirB)
 
bool md_enable_stepper (const md_context dev, MD_STEP_DIRECTION_T dir, uint8_t speed)
 
bool md_disable_stepper (const md_context dev)
 
bool md_set_stepper_steps (const md_context dev, unsigned int steps)
 
void md_config_stepper (const md_context dev, unsigned int stepsPerRev, MD_STEP_MODE_T mode)
 

Typedefs

typedef struct _md_contextmd_context
 

Function Documentation

md_context md_init ( int  bus,
uint8_t  address 
)

MD initializer.

Parameters
busI2C bus to use
addressI2C address to use
Returns
Device context.

Here is the call graph for this function:

void md_close ( md_context  dev)

MD close.

Parameters
devDevice context.

Here is the call graph for this function:

Here is the caller graph for this function:

bool md_write_packet ( const md_context  dev,
MD_REG_T  reg,
uint8_t  data1,
uint8_t  data2 
)

Composes and writes a 3-byte packet to the controller

Parameters
devDevice context.
regRegister location
data1First byte of data
data2Second byte of data
Returns
True if successful

Here is the caller graph for this function:

bool md_set_motor_speeds ( const md_context  dev,
uint8_t  speedA,
uint8_t  speedB 
)

To control DC motors, sets the speed of motors A & B. Valid values are 0-255.

Parameters
devDevice context.
speedASpeed of motor A
speedBSpeed of motor B
Returns
True if successful

Here is the call graph for this function:

Here is the caller graph for this function:

bool md_set_pwm_frequency_prescale ( const md_context  dev,
uint8_t  freq 
)

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.

Parameters
devDevice context.
freqPWM prescale frequency; default should be 0x03
Returns
True if successful

Here is the call graph for this function:

Here is the caller graph for this function:

bool md_set_motor_directions ( const md_context  dev,
MD_DC_DIRECTION_T  dirA,
MD_DC_DIRECTION_T  dirB 
)

To control DC motors, sets the directions of motors A & B

Parameters
devDevice context.
dirADirection for motor A, MD_DIR_CW or MD_DIR_CCW
dirBDirection for motor B, MD_DIR_CW or MD_DIR_CCW
Returns
True if successful

Here is the call graph for this function:

Here is the caller graph for this function:

bool md_enable_stepper ( const md_context  dev,
MD_STEP_DIRECTION_T  dir,
uint8_t  speed 
)

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 md_set_stepper_steps() has completed.

Parameters
devDevice context.
dirDirection, MD_STEP_DIR_CW or MD_STEP_DIR_CCW
speedMotor 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.
Returns
True if successful

Here is the call graph for this function:

Here is the caller graph for this function:

bool md_disable_stepper ( const md_context  dev)

To control a stepper motor, stops the stepper motor.

Parameters
devDevice context.
Returns
True if successful

Here is the call graph for this function:

Here is the caller graph for this function:

bool md_set_stepper_steps ( const md_context  dev,
unsigned int  steps 
)

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.

Parameters
devDevice context.
stepsNumber of steps to execute. 255 (only in Mode2) means continuous rotation.
Returns
True if successful

Here is the call graph for this function:

Here is the caller graph for this function:

void md_config_stepper ( const md_context  dev,
unsigned int  stepsPerRev,
MD_STEP_MODE_T  mode 
)

Configure the initial Stepper parameters. This should be called before any other stepper method.

Parameters
devDevice context.
stepsPerRevThe number of steps required to complete one full revolution.
modeThe stepper operating mode. MD_STEP_MODE1 is more flexible, and is handled directly by this driver. MD_STEP_MODE2 is handled completely by the MCU on board the motor driver, but is much more restrictive and requires updated firmware. We generally recommend MODE1.

Here is the caller graph for this function:

Typedef Documentation

typedef struct _md_context * md_context

Device context