upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
smartdrive.hpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Author: Oussema Harbi <oussema.elharbi@gmail.com>
5  * Copyright (c) <2016> <Oussema Harbi>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include <mraa/i2c.hpp>
28 
29 //We can use direct integer IDs,
30 //or we can use the typedef here to help limit the error cases
31 //and still support extension to support more Motors in the future
32 //But when using typedef, we need to cast these when sending them in i2c bus
33 #define SmartDrive_Motor_ID_1 0x01
34 #define SmartDrive_Motor_ID_2 0x02
35 #define SmartDrive_Motor_ID_BOTH 0x03
36 
37 #define SmartDrive_Dir_Reverse 0x00
38 #define SmartDrive_Dir_Forward 0x01
39 
40 #define SmartDrive_Action_Float 0x00 //stop and let the motor coast.
41 #define SmartDrive_Action_Brake 0x01 //apply brakes, and resist change to tachometer
42 #define SmartDrive_Action_BrakeHold 0x02 //apply brakes, and restore externally forced change to tachometer
43 
44 //Next action (upon completion of current action)
45 #define SmartDrive_Completion_Wait_For 0x01
46 #define SmartDrive_Completion_Dont_Wait 0x00
47 
48 #define SmartDrive_DefaultAddress 0x1b
49 #define SmartDrive_VOLTAGE_MULTIPLIER 212.7
50 
51 //Commonly used speed constants, these are just convenience constants
52 //You can use any value between 0 and 100.
53 #define SmartDrive_Speed_Full 90
54 #define SmartDrive_Speed_Medium 60
55 #define SmartDrive_Speed_Slow 25
56 
57 //Different commands
58 #define SmartDrive_CONTROL_SPEED 0x01
59 #define SmartDrive_CONTROL_RAMP 0x02
60 #define SmartDrive_CONTROL_RELATIVE 0x04
61 #define SmartDrive_CONTROL_TACHO 0x08
62 #define SmartDrive_CONTROL_BRK 0x10
63 #define SmartDrive_CONTROL_ON 0x20
64 #define SmartDrive_CONTROL_TIME 0x40
65 #define SmartDrive_CONTROL_GO 0x80
66 
67 #define SmartDrive_COMMAND 0x41
68 #define SmartDrive_SETPT_M1 0x42
69 #define SmartDrive_SPEED_M1 0x46
70 #define SmartDrive_TIME_M1 0x47
71 #define SmartDrive_CMD_B_M1 0x48
72 #define SmartDrive_CMD_A_M1 0x49
73 
74 #define SmartDrive_SETPT_M2 0x4A
75 #define SmartDrive_SPEED_M2 0x4E
76 #define SmartDrive_TIME_M2 0x4F
77 #define SmartDrive_CMD_B_M2 0x50
78 #define SmartDrive_CMD_A_M2 0x51
79 
80 //Read registers.
81 #define SmartDrive_POSITION_M1 0x52
82 #define SmartDrive_POSITION_M2 0x56
83 #define SmartDrive_STATUS_M1 0x5A
84 #define SmartDrive_STATUS_M2 0x5B
85 #define SmartDrive_TASKS_M1 0x5C
86 #define SmartDrive_TASKS_M2 0x5D
87 
88 //PID control registers
89 #define SmartDrive_P_Kp 0x5E //proportional gain-position
90 #define SmartDrive_P_Ki 0x60 //integral gain-position
91 #define SmartDrive_P_Kd 0x62 //derivative gain-position
92 #define SmartDrive_S_Kp 0x64 //proportional gain-speed
93 #define SmartDrive_S_Ki 0x66 //integral gain-speed
94 #define SmartDrive_S_Kd 0x68 //derivative gain-speed
95 #define SmartDrive_PASSCOUNT 0x6A
96 #define SmartDrive_PASSTOLERANCE 0x6B
97 
98 #define SmartDrive_CHKSUM 0x6C
99 
100 //Power data registers
101 #define SmartDrive_BATT_VOLTAGE 0x6E
102 #define SmartDrive_RESETSTATUS 0x6F
103 #define SmartDrive_CURRENT_M1 0x70
104 #define SmartDrive_CURRENT_M2 0x72
105 
106 //Supported I2C commands
107 #define CMD_R 0x52
108 #define CMD_S 0x53
109 #define CMD_a 0x61
110 #define CMD_b 0x62
111 #define CMD_c 0x63
112 #define CMD_A 0x41
113 #define CMD_B 0x42
114 #define CMD_C 0x43
115 
116 //Motor Status Masks
117 #define SmartDrive_MOTOR_CONTROL_ON 0x1
118 #define SmartDrive_MOTOR_IS_RAMPING 0x2
119 #define SmartDrive_MOTOR_IS_POWERED 0x4
120 #define SmartDrive_MOTOR_POS_CTRL_ON 0x8
121 #define SmartDrive_MOTOR_IN_BRAKE_MODE 0x10
122 #define SmartDrive_MOTOR_OVERLOADED 0x20
123 #define SmartDrive_MOTOR_IN_TIME_MODE 0x40
124 #define SmartDrive_MOTOR_IS_STALLED 0x80
125 
126 namespace upm {
127 
151 //Class definition
152 class SmartDrive {
153 
154 public:
160  SmartDrive(int i2c_bus, int address = SmartDrive_DefaultAddress);
161 
166  void command(uint8_t cmd);
167 
171  float GetBattVoltage();
172 
177  uint32_t ReadTachometerPosition(int motor_id);
178 
185  void Run_Unlimited(int motor_id, int direction, uint8_t speed);
186 
192  void StopMotor(int motor_id, int next_action );
193 
203  void Run_Seconds(int motor_id, int direction, uint8_t speed, uint8_t duration, bool wait_for_completion, int next_action );
204 
209  void WaitUntilTimeDone(int motor_id);
210 
215  bool IsTimeDone(int motor_id);
216 
226  void Run_Degrees(int motor_id, int direction, uint8_t speed, uint32_t degrees, bool wait_for_completion, int next_action);
227 
237  void Run_Rotations(int motor_id, int direction, uint8_t speed, uint32_t rotations, bool wait_for_completion, int next_action);
238 
247  void Run_Tacho(int motor_id, uint8_t speed, uint32_t tacho_count, bool wait_for_completion, int next_action);
248 
253  void WaitUntilTachoDone(int motor_id);
254 
259  bool IsTachoDone(int motor_id);
260 
270  void SetPerformanceParameters( uint16_t Kp_tacho, uint16_t Ki_tacho, uint16_t Kd_tacho, uint16_t Kp_speed, uint16_t Ki_speed, uint16_t Kd_speed, uint8_t passcount, uint8_t tolerance);
271 
276 
281  uint8_t GetMotorStatus(int motor_id);
282 
287  void PrintMotorStatus(int motor_id);
288 
289 private:
290  void writeByte(uint8_t addr, uint8_t value);
291  void writeArray(uint8_t* array, uint8_t len);
292  uint8_t readByte(uint8_t addr);
293  uint16_t readInteger(uint8_t addr);
294  uint32_t readLongSigned(uint8_t addr);
295 
296 private:
297  int m_smartdrive_control_address;
298  mraa::I2c m_i2c_smartdrive_control;
299 
300 };
301 
302 }
void Run_Unlimited(int motor_id, int direction, uint8_t speed)
Definition: smartdrive.cxx:134
void Run_Rotations(int motor_id, int direction, uint8_t speed, uint32_t rotations, bool wait_for_completion, int next_action)
Definition: smartdrive.cxx:259
void Run_Tacho(int motor_id, uint8_t speed, uint32_t tacho_count, bool wait_for_completion, int next_action)
Definition: smartdrive.cxx:300
float GetBattVoltage()
Definition: smartdrive.cxx:107
SmartDrive(int i2c_bus, int address=SmartDrive_DefaultAddress)
Definition: smartdrive.cxx:37
API for the SmartDrive advanced motor controller from OpenElectronis.
Definition: smartdrive.hpp:152
void Run_Degrees(int motor_id, int direction, uint8_t speed, uint32_t degrees, bool wait_for_completion, int next_action)
Definition: smartdrive.cxx:218
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
void SetPerformanceParameters(uint16_t Kp_tacho, uint16_t Ki_tacho, uint16_t Kd_tacho, uint16_t Kp_speed, uint16_t Ki_speed, uint16_t Kd_speed, uint8_t passcount, uint8_t tolerance)
Definition: smartdrive.cxx:357
void StopMotor(int motor_id, int next_action)
Definition: smartdrive.cxx:159
void Run_Seconds(int motor_id, int direction, uint8_t speed, uint8_t duration, bool wait_for_completion, int next_action)
Definition: smartdrive.cxx:167
uint32_t ReadTachometerPosition(int motor_id)
Definition: smartdrive.cxx:120
void WaitUntilTimeDone(int motor_id)
Definition: smartdrive.cxx:200
void WaitUntilTachoDone(int motor_id)
Definition: smartdrive.cxx:337
bool IsTachoDone(int motor_id)
Definition: smartdrive.cxx:344
void ReadPerformanceParameters()
Definition: smartdrive.cxx:377
bool IsTimeDone(int motor_id)
Definition: smartdrive.cxx:207
uint8_t GetMotorStatus(int motor_id)
Definition: smartdrive.cxx:393
void PrintMotorStatus(int motor_id)
Definition: smartdrive.cxx:406
void command(uint8_t cmd)
Definition: smartdrive.cxx:100