MatrixMiniR4 1.1.9
Matrix Mini R4 Arduino Library API Documentation
Loading...
Searching...
No Matches
MiniR4DC.h
Go to the documentation of this file.
1
6
7#ifndef MINIR4DC_H
8#define MINIR4DC_H
9
10#include "MMLower.h"
11
20template<uint8_t ID> class MiniR4DC
21{
22public:
23 MiniR4DC() { _id = ID; }
24
35 bool begin(void)
36 {
37 MMLower::RESULT result = mmL.SetDCMotorSpeedRange(_id, 0, 100);
38 MMLower::RESULT result1 = mmL.SetEncoderResetCounter(_id);
39 MMLower::RESULT result2 = mmL.SetDCMotorPower(_id, 0);
40 MMLower::RESULT result3 = mmL.SetDCMotorDir(_id, MMLower::DIR::FORWARD);
41
42 //return (result2 == MMLower::RESULT::OK);
43
44 return (
45 result == MMLower::RESULT::OK && result1 == MMLower::RESULT::OK &&
46 result2 == MMLower::RESULT::OK && result3 == MMLower::RESULT::OK);
47
48 }
49
60 bool setPPR_RPM(uint16_t motorPPR, uint16_t motorMaxRPM)
61 {
62 MMLower::RESULT result = mmL.SetEncode_PPR_MaxRPM(_id, motorPPR, motorMaxRPM);
63 return (result == MMLower::RESULT::OK);
64 }
65
72 bool setReverse(bool dir)
73 {
75 MMLower::RESULT resultMotor = mmL.SetDCMotorDir(_id, _dir);
76 // MMLower::RESULT resultEnc = mmL.SetEncoderDir(_id, _dir);
77 // return (resultMotor == MMLower::RESULT::OK && resultEnc == MMLower::RESULT::OK);
78 return (resultMotor == MMLower::RESULT::OK);
79 }
80
90 bool setPower(int16_t power)
91 {
92 MMLower::RESULT result = mmL.SetDCMotorPower(_id, power);
93
94 if(result != MMLower::RESULT::OK){
95 delay(1);
96 result = mmL.SetDCMotorPower(_id, power);
97 }
98
99 return (result == MMLower::RESULT::OK);
100 }
101
111 bool setSpeed(int16_t speed)
112 {
113 MMLower::RESULT result = mmL.SetDCMotorSpeed(_id, speed);
114 return (result == MMLower::RESULT::OK);
115 }
116
128 bool rotateFor(int16_t speed, uint16_t degree)
129 {
130 MMLower::RESULT result = mmL.SetDCMotorRotate(_id, speed, degree);
131 return (result == MMLower::RESULT::OK);
132 }
133
142 bool setFixSpeedPID(float kp, float ki, float kd)
143 {
144 MMLower::RESULT result = mmL.SetPIDParam(_id, 0, kp, ki, kd);
145 return (result == MMLower::RESULT::OK);
146 }
147
156 bool setRotatePID(float kp, float ki, float kd)
157 {
158 MMLower::RESULT result = mmL.SetPIDParam(_id, 1, kp, ki, kd);
159 return (result == MMLower::RESULT::OK);
160 }
161
167 int32_t getCounter(void)
168 {
169 int32_t counter = 0;
170 MMLower::RESULT result = mmL.GetEncoderCounter(_id, counter);
171 return counter;
172 }
173
174
175
181 int32_t getDegrees(void)
182 {
183 int32_t degs = 0;
184 MMLower::RESULT result = mmL.GetEncoderDegrees(_id, degs);
185 return degs;
186 }
187
193 bool resetCounter(void)
194 {
195 MMLower::RESULT result = mmL.SetEncoderResetCounter(_id);
196 return (result == MMLower::RESULT::OK);
197 }
198
207 bool setBrake(bool brake)
208 {
209 if (brake) {
210 MMLower::RESULT result = mmL.SetDCBrake(_id);
211 return (result == MMLower::RESULT::OK);
212 } else {
213 MMLower::RESULT result = mmL.SetDCMotorSpeed(_id, 0);
214 return (result == MMLower::RESULT::OK);
215 }
216 }
217
224 bool ChkRotateEnd(bool& isEnd)
225 {
226 MMLower::RESULT result = mmL.GetRotateState(_id, isEnd);
227 return (result == MMLower::RESULT::OK);
228 }
229
230private:
231 uint8_t _id;
232};
233
234#endif // MINIR4DC_H
MMLower mmL(8, 9, 57600)
Handling the Lower MCU (STM32) communication.
bool setReverse(bool dir)
Sets the direction of the DC motor.
Definition MiniR4DC.h:72
bool setFixSpeedPID(float kp, float ki, float kd)
Sets the PID parameters for fixed speed control. (For SetSpeed())
Definition MiniR4DC.h:142
bool setPPR_RPM(uint16_t motorPPR, uint16_t motorMaxRPM)
Sets the encoder PPR (Pulses Per Revolution) and maximum RPM for a motor.
Definition MiniR4DC.h:60
bool resetCounter(void)
Resets the encoder counter to zero.
Definition MiniR4DC.h:193
bool setBrake(bool brake)
Sets the brake mode for the DC motor.
Definition MiniR4DC.h:207
MiniR4DC()
Definition MiniR4DC.h:23
bool setSpeed(int16_t speed)
Sets the speed of the DC motor.
Definition MiniR4DC.h:111
bool setPower(int16_t power)
Sets the power level of the DC motor.
Definition MiniR4DC.h:90
int32_t getCounter(void)
Gets the current encoder counter value. (Not Degree)
Definition MiniR4DC.h:167
int32_t getDegrees(void)
Gets the current rotation in degrees based on the encoder count.
Definition MiniR4DC.h:181
bool ChkRotateEnd(bool &isEnd)
Checks if the rotation has ended.
Definition MiniR4DC.h:224
bool rotateFor(int16_t speed, uint16_t degree)
Rotates the DC motor for a specific degree at a given speed.
Definition MiniR4DC.h:128
bool setRotatePID(float kp, float ki, float kd)
Sets the PID parameters for rotation control. (For rotateFor())
Definition MiniR4DC.h:156
bool begin(void)
Initializes the DC motor settings.
Definition MiniR4DC.h:35