MatrixMiniR4 1.1.5
Matrix Mini R4 Arduino Library API Documentation
Loading...
Searching...
No Matches
MiniR4DC.h
Go to the documentation of this file.
1
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);
39 MMLower::RESULT result2 = mmL.SetDCMotorPower(_id, 0);
41
42 return (
43 result == MMLower::RESULT::OK && result1 == MMLower::RESULT::OK &&
44 result2 == MMLower::RESULT::OK && result3 == MMLower::RESULT::OK);
45 }
46
53 bool setReverse(bool dir)
54 {
56 MMLower::RESULT resultMotor = mmL.SetDCMotorDir(_id, _dir);
57 // MMLower::RESULT resultEnc = mmL.SetEncoderDir(_id, _dir);
58 // return (resultMotor == MMLower::RESULT::OK && resultEnc == MMLower::RESULT::OK);
59 return (resultMotor == MMLower::RESULT::OK);
60 }
61
71 bool setPower(int16_t power)
72 {
73 MMLower::RESULT result = mmL.SetDCMotorPower(_id, power);
74 return (result == MMLower::RESULT::OK);
75 }
76
86 bool setSpeed(int16_t speed)
87 {
88 MMLower::RESULT result = mmL.SetDCMotorSpeed(_id, speed);
89 return (result == MMLower::RESULT::OK);
90 }
91
103 bool rotateFor(int16_t speed, uint16_t degree)
104 {
105 MMLower::RESULT result = mmL.SetDCMotorRotate(_id, speed, degree);
106 return (result == MMLower::RESULT::OK);
107 }
108
117 bool setFixSpeedPID(float kp, float ki, float kd)
118 {
119 MMLower::RESULT result = mmL.SetPIDParam(_id, 0, kp, ki, kd);
120 return (result == MMLower::RESULT::OK);
121 }
122
131 bool setRotatePID(float kp, float ki, float kd)
132 {
133 MMLower::RESULT result = mmL.SetPIDParam(_id, 1, kp, ki, kd);
134 return (result == MMLower::RESULT::OK);
135 }
136
142 int32_t getCounter(void)
143 {
144 int32_t counter = 0;
145 MMLower::RESULT result = mmL.GetEncoderCounter(_id, counter);
146 return counter;
147 }
148
154 int32_t getDegrees(void)
155 {
156 int32_t counter = 0;
157 MMLower::RESULT result = mmL.GetEncoderCounter(_id, counter);
158 return (int32_t)((double)counter / 545 * 360);
159 }
160
166 bool resetCounter(void)
167 {
169 return (result == MMLower::RESULT::OK);
170 }
171
180 bool setBrake(bool brake)
181 {
182 if (brake) {
183 MMLower::RESULT result = mmL.SetDCBrake(_id);
184 return (result == MMLower::RESULT::OK);
185 } else {
186 MMLower::RESULT result = mmL.SetDCMotorSpeed(_id, 0);
187 return (result == MMLower::RESULT::OK);
188 }
189 }
190
197 bool ChkRotateEnd(bool& isEnd)
198 {
199 MMLower::RESULT result = mmL.GetRotateState(_id, isEnd);
200 return (result == MMLower::RESULT::OK);
201 }
202
203private:
204 uint8_t _id;
205};
206
207#endif // MINIR4DC_H
MMLower mmL(8, 9, 57600)
Handling the Lower MCU (STM32) communication.
RESULT GetRotateState(uint8_t num, bool &isEnd)
Definition MMLower.cpp:907
RESULT SetDCMotorPower(uint8_t num, int16_t power)
Definition MMLower.cpp:327
RESULT SetDCMotorRotate(uint8_t num, int16_t maxSpeed, uint16_t degree)
Definition MMLower.cpp:393
RESULT GetEncoderCounter(uint8_t num, int32_t &enCounter)
Definition MMLower.cpp:767
RESULT SetDCMotorSpeed(uint8_t num, int16_t speed)
Definition MMLower.cpp:360
RESULT SetEncoderResetCounter(uint8_t num)
Definition MMLower.cpp:593
RESULT SetPIDParam(uint8_t num, uint8_t pidNum, float kp, float ki, float kd)
Definition MMLower.cpp:618
RESULT SetDCMotorSpeedRange(uint8_t num, uint16_t min, uint16_t max)
Definition MMLower.cpp:113
RESULT SetDCBrake(uint8_t num)
Definition MMLower.cpp:644
RESULT SetDCMotorDir(uint8_t num, DIR dir)
Definition MMLower.cpp:36
Class for controlling a DC motor with encoder functionality.
Definition MiniR4DC.h:21
bool setReverse(bool dir)
Sets the direction of the DC motor.
Definition MiniR4DC.h:53
bool setFixSpeedPID(float kp, float ki, float kd)
Sets the PID parameters for fixed speed control. (For SetSpeed())
Definition MiniR4DC.h:117
bool resetCounter(void)
Resets the encoder counter to zero.
Definition MiniR4DC.h:166
bool setBrake(bool brake)
Sets the brake mode for the DC motor.
Definition MiniR4DC.h:180
MiniR4DC()
Definition MiniR4DC.h:23
bool setSpeed(int16_t speed)
Sets the speed of the DC motor.
Definition MiniR4DC.h:86
bool setPower(int16_t power)
Sets the power level of the DC motor.
Definition MiniR4DC.h:71
int32_t getCounter(void)
Gets the current encoder counter value. (Not Degree)
Definition MiniR4DC.h:142
int32_t getDegrees(void)
Gets the current rotation in degrees based on the encoder count.
Definition MiniR4DC.h:154
bool ChkRotateEnd(bool &isEnd)
Checks if the rotation has ended.
Definition MiniR4DC.h:197
bool rotateFor(int16_t speed, uint16_t degree)
Rotates the DC motor for a specific degree at a given speed.
Definition MiniR4DC.h:103
bool setRotatePID(float kp, float ki, float kd)
Sets the PID parameters for rotation control. (For rotateFor())
Definition MiniR4DC.h:131
bool begin(void)
Initializes the DC motor settings.
Definition MiniR4DC.h:35