33#include " utils/Debug.hpp"
44#include " utils/Regulator.hpp"
55
6- #include < algorithm>
76#include < stdint.h>
87
98inline const uint16_t motorLoopFreq = 100 ;
109
1110class Motor {
1211public:
13- Motor ()
14- : m_velocityReg(INT16_MAX, 150000 , 300000 , 20000 )
15- , m_positionReg(INT16_MAX, 150000 , 300000 , 20000 )
16- , m_state(PowerCtrl) {}
17-
18- std::pair<int16_t , bool > poll (uint16_t encTicks) {
19- int16_t actualTicksPerLoop = encTicks - m_lastEncTicks;
20- m_actualPosition += actualTicksPerLoop;
12+ Motor () {
13+ m_lastEncTicks = 0 ;
14+ reset ();
15+ }
16+
17+ void reset () {
18+ m_velocityReg = Regulator (INT16_MAX, 150000 , 300000 , 20000 );
19+ m_positionReg = Regulator (500 , 1000 , 0 , 0 );
20+ m_dither = 0 ;
21+ m_targetVelocity = 0 ;
22+ m_actualPower = 0 ;
23+ m_actualPosition = 0 ;
24+ m_targetPosition = 0 ;
25+ m_actualTicksPerLoop = 0 ;
26+ m_posEpsilon = 3 ;
27+ m_velEpsilon = 3 ;
28+ m_maxAccel = 2000 / motorLoopFreq;
29+ m_mode = MotorMode_POWER;
30+ }
31+
32+ bool atTargetPosition () const {
33+ return uint32_t (abs (m_actualPosition - m_targetPosition))
34+ <= m_posEpsilon;
35+ }
36+
37+ bool atStandstill () const {
38+ return uint32_t (abs (m_actualTicksPerLoop)) <= m_velEpsilon;
39+ }
40+
41+ MotorMode mode () const { return m_mode; }
42+ void modeChange (MotorMode newMode) { m_mode = newMode; }
43+
44+ void reportStat (CoprocStat_MotorStat& stat) {
45+ stat.mode = m_mode;
46+ stat.position = m_actualPosition;
47+ stat.power = m_actualPower;
48+ stat.velocity = m_actualTicksPerLoop * motorLoopFreq;
49+ };
50+
51+ int16_t poll (uint16_t encTicks) {
52+ m_actualTicksPerLoop = encTicks - m_lastEncTicks;
53+ m_actualPosition += m_actualTicksPerLoop;
2154 m_lastEncTicks = encTicks;
22- if (m_state == PowerCtrl) {
23- return std::make_pair (m_targetPower, false );
24- } else if (m_state == BrakeCtrl) {
25- return std::make_pair (m_targetBrakingPower, true );
26- } else if (m_state == VelocityCtrl) {
55+
56+ switch (m_mode) {
57+ case MotorMode_POSITION:
58+ case MotorMode_POSITION_IDLE: {
59+ // DEBUG("i:%ld e:%ld s:%ld -> v:%ld\n", m_positionReg.integrator(),
60+ // m_positionReg.e(), m_actualPosition, m_positionReg.output());
61+ if (atTargetPosition () && atStandstill ()) {
62+ m_positionReg.clear ();
63+ m_targetVelocity = 0 ;
64+ modeChange (MotorMode_POSITION_IDLE);
65+ } else {
66+ auto action
67+ = m_positionReg.process (m_targetPosition, m_actualPosition);
68+
69+ // Limit ramp-up to max acceleration
70+ if (action > m_targetVelocity + m_maxAccel) {
71+ m_targetVelocity += m_maxAccel;
72+ } else if (action < m_targetVelocity - m_maxAccel) {
73+ m_targetVelocity -= m_maxAccel;
74+ } else {
75+ m_targetVelocity = action;
76+ }
77+ }
78+ } // fallthrough
79+ case MotorMode_VELOCITY: {
2780 int16_t targetTicksPerLoop = m_targetVelocity / motorLoopFreq;
2881 uint16_t targetTicksRem = abs (m_targetVelocity % motorLoopFreq);
2982 if ((targetTicksRem * 4 ) / motorLoopFreq > m_dither) {
@@ -33,35 +86,34 @@ class Motor {
3386 m_dither = 0 ;
3487 }
3588
36- auto action
37- = m_velocityReg.process (targetTicksPerLoop, actualTicksPerLoop);
38- return std::make_pair (action, false );
39- } else if (m_state == PositionCtrl) {
40- auto action
41- = m_positionReg.process (m_targetPosition, m_actualPosition);
42- return std::make_pair (action, false );
89+ auto action = m_velocityReg.process (
90+ targetTicksPerLoop, m_actualTicksPerLoop);
91+ m_actualPower = action;
92+ } break ;
93+ default :
94+ break ;
4395 }
44- return std::make_pair ( 0 , false ) ;
96+ return m_actualPower ;
4597 }
4698
4799 void setTargetPower (int16_t power) {
48- if (m_state != PowerCtrl ) {
49- m_state = PowerCtrl ;
100+ if (m_mode != MotorMode_POWER ) {
101+ modeChange (MotorMode_POWER) ;
50102 }
51- m_targetPower = power;
103+ m_actualPower = power;
52104 }
53105
54106 void setTargetBrakingPower (int16_t brakingPower) {
55- if (m_state != BrakeCtrl ) {
56- m_state = BrakeCtrl ;
107+ if (m_mode != MotorMode_BRAKE ) {
108+ modeChange (MotorMode_BRAKE) ;
57109 }
58- m_targetBrakingPower = brakingPower;
110+ m_actualPower = brakingPower;
59111 }
60112
61113 void setTargetVelocity (int16_t ticksPerSec) {
62- if (m_state != VelocityCtrl ) {
114+ if (m_mode != MotorMode_VELOCITY ) {
63115 m_velocityReg.clear ();
64- m_state = VelocityCtrl ;
116+ modeChange (MotorMode_VELOCITY) ;
65117 }
66118 m_targetVelocity = ticksPerSec;
67119 }
@@ -71,50 +123,52 @@ class Motor {
71123 m_actualPosition = homedTicks;
72124 }
73125
74- void setTargetPosition (int32_t ticks) {
75- if (m_state != PositionCtrl) {
126+ void setTargetPosition (
127+ const CoprocReq_MotorReq_SetPosition& req, bool additive) {
128+ if (m_mode != MotorMode_POSITION) {
76129 m_positionReg.clear ();
77- m_state = PositionCtrl ;
130+ modeChange (MotorMode_POSITION) ;
78131 }
79- m_targetPosition = ticks;
80- }
81-
82- void addTargetPosition (int32_t ticksDelta) {
83- if (m_state != PositionCtrl) {
84- m_positionReg.clear ();
85- m_state = PositionCtrl;
132+ if (additive) {
133+ m_targetPosition += req.targetPosition ;
134+ } else {
135+ m_targetPosition = req.targetPosition ;
86136 }
87- m_targetPosition += ticksDelta ;
137+ m_positionReg. setMaxOutput (req. runningVelocity ) ;
88138 }
89139
90- void setVelocityPid (uint32_t p, uint32_t i, uint32_t d ) {
91- m_velocityReg.setP (p);
92- m_velocityReg.setI (i);
93- m_velocityReg.setD (d);
140+ void setVelocityPid (const RegCoefs& coefs ) {
141+ m_velocityReg.setP (coefs. p );
142+ m_velocityReg.setI (coefs. i );
143+ m_velocityReg.setD (coefs. d );
94144 m_velocityReg.clear ();
95145 }
96146
97- void setPositionPid (uint32_t p, uint32_t i, uint32_t d ) {
98- m_positionReg.setP (p);
99- m_positionReg.setI (i);
100- m_positionReg.setD (d);
147+ void setPositionPid (const RegCoefs& coefs ) {
148+ m_positionReg.setP (coefs. p );
149+ m_positionReg.setI (coefs. i );
150+ m_positionReg.setD (coefs. d );
101151 m_positionReg.clear ();
102152 }
103153
154+ void setConfig (const MotorConfig& config) {
155+ m_velEpsilon = config.velEpsilon ;
156+ m_posEpsilon = config.posEpsilon ;
157+ m_maxAccel = config.maxAccel / motorLoopFreq;
158+ }
159+
104160private:
105161 Regulator m_velocityReg;
106162 Regulator m_positionReg;
107- int16_t m_targetPower;
108- int16_t m_targetBrakingPower;
163+ int16_t m_actualPower;
109164 int16_t m_targetVelocity;
110165 int32_t m_targetPosition;
111166 int32_t m_actualPosition;
167+ int16_t m_actualTicksPerLoop;
112168 uint16_t m_dither;
113169 uint16_t m_lastEncTicks;
114- enum MotorState {
115- PowerCtrl,
116- BrakeCtrl,
117- VelocityCtrl,
118- PositionCtrl,
119- } m_state;
170+ uint16_t m_posEpsilon;
171+ uint16_t m_velEpsilon;
172+ uint16_t m_maxAccel;
173+ MotorMode m_mode;
120174};
0 commit comments