upm  1.0.2
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
md.h
Go to the documentation of this file.
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <upm.h>
27 #include <mraa/i2c.h>
28 
29 #define MD_I2C_BUS 0
30 #define MD_DEFAULT_I2C_ADDR 0x0f
31 
32 // This is a NOOP value used to pad packets
33 #define MD_NOOP 0x01
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
47  // MD registers
48  typedef enum {
49  MD_REG_SET_SPEED = 0x82,
50  MD_REG_SET_PWM_FREQ = 0x84,
51  MD_REG_SET_DIRECTION = 0xaa,
52  MD_REG_SET_MOTOR_A = 0xa1, // not documented
53  MD_REG_SET_MOTOR_B = 0xa5, // not documented
54  MD_REG_STEPPER_ENABLE = 0x1a,
55  MD_REG_STEPPER_DISABLE = 0x1b,
56  MD_REG_STEPPER_NUM_STEPS = 0x1c
57  } MD_REG_T;
58 
59  // legal directions for the stepper
60  typedef enum {
61  MD_STEP_DIR_CCW = 0x01,
62  MD_STEP_DIR_CW = 0x00
63  } MD_STEP_DIRECTION_T;
64 
65  // legal directions for individual DC motors
66  typedef enum {
67  MD_DIR_CCW = 0x02,
68  MD_DIR_CW = 0x01
69  } MD_DC_DIRECTION_T;
70 
71  // stepper modes
72  typedef enum {
73  MD_STEP_MODE1 = 0x00,
74  MD_STEP_MODE2 = 0x01
75  } MD_STEP_MODE_T;
76 
80  typedef struct _md_context {
81  mraa_i2c_context i2c;
82 
83  // steps per revolution
84  int stepsPerRev;
85  int currentStep;
86  uint32_t stepDelay;
87  uint32_t totalSteps;
88  MD_STEP_MODE_T stepMode;
89 
90  // step direction: - 1 = forward, -1 = backward
91  int stepDirection;
92 
93  // initialized?
94  bool initialized;
95 
96  } *md_context;
97 
105  md_context md_init(int bus, uint8_t address);
106 
112  void md_close(md_context dev);
113 
123  bool md_write_packet(const md_context dev, MD_REG_T reg, uint8_t data1,
124  uint8_t data2);
125 
135  bool md_set_motor_speeds(const md_context dev, uint8_t speedA,
136  uint8_t speedB);
137 
148  bool md_set_pwm_frequency_prescale(const md_context dev, uint8_t freq);
149 
158  bool md_set_motor_directions(const md_context dev, MD_DC_DIRECTION_T dirA,
159  MD_DC_DIRECTION_T dirB);
160 
176  bool md_enable_stepper(const md_context dev, MD_STEP_DIRECTION_T dir,
177  uint8_t speed);
178 
185  bool md_disable_stepper(const md_context dev);
186 
199  bool md_set_stepper_steps(const md_context dev, unsigned int steps);
200 
214  void md_config_stepper(const md_context dev, unsigned int stepsPerRev,
215  MD_STEP_MODE_T mode);
216 
217 
218 #ifdef __cplusplus
219 }
220 #endif
void md_config_stepper(const md_context dev, unsigned int stepsPerRev, MD_STEP_MODE_T mode)
Definition: md.c:254
bool md_enable_stepper(const md_context dev, MD_STEP_DIRECTION_T dir, uint8_t speed)
Definition: md.c:179
struct _md_context * md_context
bool md_disable_stepper(const md_context dev)
Definition: md.c:221
bool md_set_motor_speeds(const md_context dev, uint8_t speedA, uint8_t speedB)
Definition: md.c:156
bool md_write_packet(const md_context dev, MD_REG_T reg, uint8_t data1, uint8_t data2)
Definition: md.c:129
Definition: md.h:80
void md_close(md_context dev)
Definition: md.c:113
bool md_set_pwm_frequency_prescale(const md_context dev, uint8_t freq)
Definition: md.c:163
md_context md_init(int bus, uint8_t address)
Definition: md.c:60
bool md_set_stepper_steps(const md_context dev, unsigned int steps)
Definition: md.c:233
bool md_set_motor_directions(const md_context dev, MD_DC_DIRECTION_T dirA, MD_DC_DIRECTION_T dirB)
Definition: md.c:170