-
Notifications
You must be signed in to change notification settings - Fork 0
/
CART.h
64 lines (61 loc) · 1.54 KB
/
CART.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
#ifndef CART_HEADER
#define CART_HEADER
#include "MOTOR.h"
class CART {
public:
CART(MOTOR& motor, float length = 2.0);
/*
* Sets up Timer/Counter 2 to quadrature demodulation mode
* PHA on pin 5
* PHB on pin 4
*
* Left switch pin 6
* Right switch pin 7
*/
void begin();
/*
* Reads current value from counter
*/
int32_t read_raw();
/*
* Reads current position realative to center of rail
* Output between -length/2 and length/2
* Do not use without performing a calibration first!
*/
float read();
/*
* Performs calibration by moving cart between ends of rail
* Optional parameters specify voltage to use when
* quicly moving from to each end and slowly ensure precise
* positioning on switch. Low enough to not ram into end, high
* enough to overcome static friction.
*/
void calibrate(float slow = 3.5, float fast = 5.5);
/*
* Tries to move cart to requested position
* uses proportional controller with optional given Kp
* position from -rail_length/2 to rail_length/2
* Stops when error <= 0.01 or edge switch is hit
*
* Return true if edge was hit during the move
*/
bool move_to(float position, float P = 550.0);
/*
* Check if cart has hit edge switch
*/
bool edge_hit();
/*
* Half of length of rail in meters
*/
float rail_length;
/*
* Half of rail limit for move_to
*/
float rail_limit;
private:
MOTOR& motor;
int32_t left_side, right_side;
enum side {LEFT, RIGHT};
void reach(float slow, float fast, enum side side);
};
#endif