-
Notifications
You must be signed in to change notification settings - Fork 2
/
motor.hpp
166 lines (129 loc) · 3.77 KB
/
motor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef Arduino_h
#include "Arduino.h"
#endif
#ifndef MODULE_H
#include "module.hpp"
#endif
#ifndef TimerOne_h_
#include "TimerOne.h"
#endif
#ifndef SWITCH_COUNT
#define SWITCH_COUNT 4
#endif
#ifndef MOTOR_H
#define MOTOR_H
#define PIN_COUNT 6
#include <assert.h>
#include <stdint.h>
#include "timeouts.h"
typedef struct MOTOR_SPEED {
float left;
float right;
} MotorSpeed;
enum STATES {
FORWARD = 0,
REVERSE = 1,
BRAKE = 2,
COAST = 3,
TURN_LEFT = 4,
TURN_RIGHT = 5,
PIVOT_LEFT = 6,
PIVOT_RIGHT = 7,
LENGTH = 8
};
enum WHEELS {
WHEEL_LEFT,
WHEEL_RIGHT
};
class Motor: public RobotModule {
private:
// IO pins
uint8_t pinENA;
uint8_t pinIN1;
uint8_t pinIN2;
uint8_t pinENB;
uint8_t pinIN3;
uint8_t pinIN4;
uint8_t channelA;
uint8_t channelB;
uint8_t channelC;
uint8_t channelD;
uint8_t switchCount;
uint8_t *switchPins;
// State and speed machine
int previousState;
int currentState;
bool started = false;
bool isAtMole = false;
unsigned long lastSpeedUpdate = 0;
const unsigned int SPEED_UPDATE_INTERVAL = 1000; // 1ms
const unsigned int SPEED_CORRECTION_FACTOR = 1.75; // Max speed is now 1.0/1.75 = 0.5714 (57.14%)
void setPins(uint8_t enA, uint8_t in1, uint8_t in2, uint8_t enB, uint8_t in3, uint8_t in4, int state);
void setAllPins();
bool isZero(float speed);
bool isZero(float speed, float precision);
float clampSpeed(float speed);
void updatePreviousSpeed();
void handleSpeedChange();
int getPWMValue(uint8_t isEnabled, uint8_t wheel);
int speedToInt(float speed);
void startCounting();
void forward() { currentState = FORWARD; }
void reverse() { currentState = REVERSE; }
void coast() { currentState = COAST; }
void turnLeft() { currentState = TURN_LEFT; }
void turnRight() { currentState = TURN_RIGHT; }
void pivotLeft() { currentState = PIVOT_LEFT; }
void pivotRight() { currentState = PIVOT_RIGHT; }
public:
Motor(
uint8_t pin_ENA,
uint8_t pin_IN1,
uint8_t pin_IN2,
uint8_t pin_ENB,
uint8_t pin_IN3,
uint8_t pin_IN4,
uint8_t CHANNEL_A,
uint8_t CHANNEL_B,
uint8_t CHANNEL_C,
uint8_t CHANNEL_D,
uint8_t switch_count,
uint8_t *switchPins
);
~Motor();
// Internal variables
float speedChangeRate = 0.01; // 1% per tick
const int wheelbase = 218; // 218mm
const float wheelbaseMeters = 0.218; // 218mm
const float wheelbaseCm = 21.8; // 218mm
bool initialize();
bool systemsCheck();
// Actions
// This should be called in loop() continuously. It only updates the state.
void move();
// Reset the distance counters
void resetCounters();
// Sets the target wheel speed. Returns true if the speed was changed.
bool setTargetSpeed(float left, float right);
bool setTargetSpeed(MotorSpeed* speed);
// Sets true wheel speed
void setSpeed(float left, float right);
void setSpeed(MotorSpeed* speed);
float getCurrentLeftSpeed();
float getCurrentRightSpeed();
float getTargetLeftSpeed();
float getTargetRightSpeed();
void printDistance();
void printPins();
// Sets the wheels to brake until the next setSpeed() call
void brake() { currentState = BRAKE; }
MotorSpeed* calculateSpeeds(MotorSpeed* dest, float averageSpeed, float angle);
// Getters
float getLeftDistance();
float getRightDistance();
float getTrueLeftSpeed();
float getTrueRightSpeed();
void followWall(float targetDistance, float currentDistance, float radius, float averageSpeed, int position);
bool isForward() { return !(currentState == REVERSE); }
};
#endif