-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActuatorsAndSensors.h
182 lines (147 loc) · 5.17 KB
/
ActuatorsAndSensors.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**************************************************************************************
****************************************************************************************
******************************Actuators and Sensors Library*****************************
****************************************************************************************
***************************************************************************************/
//This library has a combination of actuators and sensors
//All units are in cm, so if you want different units, use a common conversion factor
#ifndef ActuatorsAndSensors_h
#define ActuatorsAndSensors_h
#include "Arduino.h"
#include <Encoder.h>
//Base class for sensor types to utilize a common read function for trigger events
class Sensor{
public:
virtual int read() = 0;
};
/**************************************************************************************
********************************Ultrasonic Sensor Class********************************
***************************************************************************************/
class UltrasonicSensor : public Sensor{
public:
UltrasonicSensor(int trigPin, int echoPin);
int read();
private:
int _trigPin;
int _echoPin;
};
/**************************************************************************************
************************************IR Sensor Class************************************
***************************************************************************************/
//Sharp IR sensor class. Returns value according to decaying exponential betwen 10-80cm
class IrSensor : public Sensor{
public:
IrSensor(int readPin);
int read();
private:
int _readPin;
};
/**************************************************************************************
***********************************Bump Switch Class***********************************
***************************************************************************************/
//Simple bump switch class. Would also work with regular switches.
class BumpSwitch : public Sensor{
public:
BumpSwitch(int pin, String type);
int read();
private:
int _pin;
String _type;
bool _NC;
};
/**************************************************************************************
*********************************Linear Actuator Class*********************************
***************************************************************************************/
//Simple Linear actuator class, no direct feedback
class LinearActuator {
public:
LinearActuator(int IN1, int IN2, int ENA);
void goUp(int pwm = 255);
void goDown(int pwm = 255);
void pause();
void turnOn();
void turnOff();
String getLog();
private:
int _IN1;
int _IN2;
int _ENA;
bool _active;
int _effort;
};
/**************************************************************************************
**************************************Motor Class**************************************
***************************************************************************************/
//DC motor class with 2 channel encoder attached
class Motor{
public:
Motor(int IN1, int IN2, int ENA, int lowerLim, int upperLim, Encoder* enc, double countPerCm, int encDir);
//Open Loop Methods
void goForward(int pwm);
void goBackward(int pwm);
void goForwardUntil(int pwm, Sensor *trigger, String condition ,int value);
void goBackwardUntil(int pwm, Sensor *trigger, String condition ,int value);
void pause();
//Power Control
void turnOn();
void turnOff();
//Closed Loop Methods
void PIDPos(double kp, double ki, double kd, double target);
void PIDVel(double kp, double ki, double kd, double target);
//Direct Read Feedback
double getPos();
double getVel();
double getAcc();
//Reseting Position
void setPos(double position); //done
//Main Loop Function that MUST be called in loop() without significant delay functions (runs on a 50 microsecond clock)
void internalUpdate(); //needs finishing
String getLog();
private:
//Pins
int _IN1;
int _IN2;
int _ENA;
//PWM Bounds
int _lowerLim;
int _upperLim;
//Motor Geometry
double _countPerCm;
//State Variables
double _position;
double _velocity;
double _acceleration;
//If power is allowed to motor or not
bool _active;
//PID Variables
bool _posTrack; //tracking velocity if false
double _posTarget;
double _velTarget;
double _kp;
double _kd;
double _ki;
bool _closedLoop;
double _iError;
double _startError;
//Encoder object pointer
Encoder* _encoder = NULL;
int _encDir;
//InternalUpdate() variables for calculations
int _polingInterval;
unsigned long _startTime;
unsigned long _currentTime;
double _startPosition;
double _startVelocity;
//Event Trigger Variables
bool _checkTrigger;
Sensor* _trigger;
int _triggerValue;
String _condition;
//Internal Functions
void goForwardInternal(int pwm); //done
void goBackwardInternal(int pwm); //done
void pauseInternal(); //done
//Extra logging
int _effort;
};
#endif