MakeBlock Drive Updated Updated library for MakeBlock Ranger

Loading...
Searching...
No Matches
MeEncoderOnBoard.h
Go to the documentation of this file.
1
73#ifndef ME_Encoder_OnBoard_H
74#define ME_Encoder_OnBoard_H
75
76#include <Arduino.h>
77#include <stdbool.h>
78#include <avr/interrupt.h>
79
80#define DIRECT_MODE 0x00
81#define PID_MODE 0x01
82#define PWM_MODE 0x02
83
84#define MOTION_WITH_POS 0x00
85#define MOTION_WITHOUT_POS 0x01
86
87#define PWM_MIN_OFFSET 25
88#define ENCODER_POS_DEADBAND 10
89#define DECELERATION_DISTANCE_PITCH 6
90
91 typedef struct
92{
93 float P, I, D;
94 float Setpoint, Output, Integral, differential, last_error;
95} PID_internal;
96
97 typedef struct
98{
99 uint8_t mode;
100 uint8_t motionState;
101
102 int16_t pulseEncoder;
103 int16_t currentPwm;
104 int16_t targetPwm;
105 int16_t previousPwm;
106
107 float currentSpeed;
108 float targetSpeed;
109 float previousSpeed;
110 float ratio;
111
112 long currentPos;
113 long targetPos;
114 long previousPos;
115 long pulsePos;
116
117 PID_internal PID_speed;
118 PID_internal PID_pos;
119}Me_Encoder_type;
120
121 typedef struct
122{
123 uint8_t port_A; //INT
124 uint8_t port_B; //DIR
125 uint8_t port_PWM; //PWM
126 uint8_t port_H1;
127 uint8_t port_H2;
128} Encoder_port_type;
129
130 extern Encoder_port_type encoder_Port[6]; // encoder_Port[0] is nonsense
131 typedef void (*cb)(int16_t,int16_t);
132
138 class MeEncoderOnBoard
139{
140 public:
147 MeEncoderOnBoard();
148
154 MeEncoderOnBoard(int slot);
155
170 void reset(uint8_t slot);
171
186 uint8_t getSlotNum(void) const;
187
202 uint8_t getIntNum(void) const;
203
218 uint8_t getPortA(void) const;
219
234 uint8_t getPortB(void) const;
235
250 long getPulsePos(void) const;
251
267 void setPulsePos(long pulse_pos);
268
283 void pulsePosPlus(void);
284
299 void pulsePosMinus(void);
300
315 void setCurrentSpeed(float speed);
316
331 float getCurrentSpeed(void) const;
332
347 int16_t getCurPwm(void) const;
348
363 void setTarPWM(int16_t pwm_value);
364
379 void setMotorPwm(int16_t pwm);
380
395 void updateSpeed(void);
396
411 void updateCurPos(void);
412
427 long getCurPos(void) const;
428
443 void runSpeed(float speed);
444
460 void setSpeed(float speed);
461
482 void move(long position,float speed = 100,int16_t extId=0,cb callback=NULL);
483
504 void moveTo(long position,float speed = 100,int16_t extId=0,cb callback=NULL);
505
521 long distanceToGo() const;
522
541 void setSpeedPid(float p,float i,float d);
542
561 void setPosPid(float p,float i,float d);
562
577 void setPulse(int16_t pulseValue);
578
593 void setRatio(float RatioValue);
594
609 void setMotionMode(int16_t motionMode);
610
625 int16_t pidPositionToPwm(void);
626
641 int16_t speedWithoutPos(void);
642
657 void encoderMove(void);
658
673 void pwmMove(void);
674
690 boolean isTarPosReached(void) const;
691
706 void loop(void);
707
708 private:
709 volatile Me_Encoder_type encode_structure;
710 boolean _Lock_flag;
711 boolean _Dir_lock_flag;
712 boolean _Callback_flag;
713 uint8_t _extId;
714 uint8_t _Port_A;
715 uint8_t _Port_B;
716 uint8_t _Port_PWM;
717 uint8_t _Port_H1;
718 uint8_t _Port_H2;
719 uint8_t _IntNum;
720 uint8_t _Slot;
721 int16_t _Encoder_output;
722 long _Measurement_speed_time;
723 long _Encoder_move_time;
724 bool _enabled;
725 cb _callback;
726};
727#endif
728
729
730
MeEncoderOnBoard
Driver for Encoder module on MeAuriga and MeMegaPi.
Definition MeEncoderOnBoard.h:139
MeEncoderOnBoard::setMotionMode
void setMotionMode(int16_t motionMode)
Definition MeEncoderOnBoard.cpp:791
MeEncoderOnBoard::isTarPosReached
boolean isTarPosReached(void) const
Definition MeEncoderOnBoard.cpp:1039
MeEncoderOnBoard::getCurPos
long getCurPos(void) const
Definition MeEncoderOnBoard.cpp:554
MeEncoderOnBoard::getIntNum
uint8_t getIntNum(void) const
Definition MeEncoderOnBoard.cpp:252
MeEncoderOnBoard::setCurrentSpeed
void setCurrentSpeed(float speed)
Definition MeEncoderOnBoard.cpp:386
MeEncoderOnBoard::moveTo
void moveTo(long position, float speed=100, int16_t extId=0, cb callback=NULL)
Definition MeEncoderOnBoard.cpp:649
MeEncoderOnBoard::speedWithoutPos
int16_t speedWithoutPos(void)
Definition MeEncoderOnBoard.cpp:926
MeEncoderOnBoard::setMotorPwm
void setMotorPwm(int16_t pwm)
Definition MeEncoderOnBoard.cpp:463
MeEncoderOnBoard::setPulsePos
void setPulsePos(long pulse_pos)
Definition MeEncoderOnBoard.cpp:329
MeEncoderOnBoard::loop
void loop(void)
Definition MeEncoderOnBoard.cpp:1058
MeEncoderOnBoard::getPortB
uint8_t getPortB(void) const
Definition MeEncoderOnBoard.cpp:290
MeEncoderOnBoard::pulsePosPlus
void pulsePosPlus(void)
Definition MeEncoderOnBoard.cpp:348
MeEncoderOnBoard::setTarPWM
void setTarPWM(int16_t pwm_value)
Definition MeEncoderOnBoard.cpp:443
MeEncoderOnBoard::setSpeedPid
void setSpeedPid(float p, float i, float d)
Definition MeEncoderOnBoard.cpp:707
MeEncoderOnBoard::getPortA
uint8_t getPortA(void) const
Definition MeEncoderOnBoard.cpp:271
MeEncoderOnBoard::distanceToGo
long distanceToGo() const
Definition MeEncoderOnBoard.cpp:684
MeEncoderOnBoard::updateSpeed
void updateSpeed(void)
Definition MeEncoderOnBoard.cpp:508
MeEncoderOnBoard::pulsePosMinus
void pulsePosMinus(void)
Definition MeEncoderOnBoard.cpp:367
MeEncoderOnBoard::getCurrentSpeed
float getCurrentSpeed(void) const
Definition MeEncoderOnBoard.cpp:405
MeEncoderOnBoard::setSpeed
void setSpeed(float speed)
Definition MeEncoderOnBoard.cpp:596
MeEncoderOnBoard::runSpeed
void runSpeed(float speed)
Definition MeEncoderOnBoard.cpp:573
MeEncoderOnBoard::getSlotNum
uint8_t getSlotNum(void) const
Definition MeEncoderOnBoard.cpp:233
MeEncoderOnBoard::setRatio
void setRatio(float RatioValue)
Definition MeEncoderOnBoard.cpp:772
MeEncoderOnBoard::pwmMove
void pwmMove(void)
Definition MeEncoderOnBoard.cpp:1011
MeEncoderOnBoard::getCurPwm
int16_t getCurPwm(void) const
Definition MeEncoderOnBoard.cpp:424
MeEncoderOnBoard::pidPositionToPwm
int16_t pidPositionToPwm(void)
Definition MeEncoderOnBoard.cpp:810
MeEncoderOnBoard::updateCurPos
void updateCurPos(void)
Definition MeEncoderOnBoard.cpp:534
MeEncoderOnBoard::getPulsePos
long getPulsePos(void) const
Definition MeEncoderOnBoard.cpp:309
MeEncoderOnBoard::setPosPid
void setPosPid(float p, float i, float d)
Definition MeEncoderOnBoard.cpp:732
MeEncoderOnBoard::reset
void reset(uint8_t slot)
Definition MeEncoderOnBoard.cpp:168
MeEncoderOnBoard::setPulse
void setPulse(int16_t pulseValue)
Definition MeEncoderOnBoard.cpp:753
MeEncoderOnBoard::move
void move(long position, float speed=100, int16_t extId=0, cb callback=NULL)
Definition MeEncoderOnBoard.cpp:623
MeEncoderOnBoard::encoderMove
void encoderMove(void)
Definition MeEncoderOnBoard.cpp:979
MeEncoderOnBoard::MeEncoderOnBoard
MeEncoderOnBoard()
Definition MeEncoderOnBoard.cpp:92
Encoder_port_type
Definition MeEncoderOnBoard.h:122
Me_Encoder_type
Definition MeEncoderOnBoard.h:98
PID_internal
Definition MeEncoderOnBoard.h:92