-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAlfredo_NoU2.h
148 lines (134 loc) · 4.07 KB
/
Alfredo_NoU2.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
#ifndef ALFREDO_NOU2_H
#define ALFREDO_NOU2_H
#include <inttypes.h>
// Pins
#define MOTOR1_A 13
#define MOTOR1_B 12
#define MOTOR2_A 27
#define MOTOR2_B 33
#define MOTOR3_A 32
#define MOTOR3_B 18
#define MOTOR4_A 19
#define MOTOR4_B 23
#define MOTOR5_A 15
#define MOTOR5_B 14
#define MOTOR6_A 22
#define MOTOR6_B 21
#define SERVO1_PIN 16
#define SERVO2_PIN 17
#define SERVO3_PIN 25
#define SERVO4_PIN 26
#define RSL_PIN 2 // Same as built-in LED
// PWM Channels
#define MOTOR1_CHANNEL 0
#define MOTOR2_CHANNEL 1
#define MOTOR3_CHANNEL 2
#define MOTOR4_CHANNEL 3
#define MOTOR5_CHANNEL 4
#define MOTOR6_CHANNEL 5
#define SERVO1_CHANNEL 6
#define SERVO2_CHANNEL 7
#define SERVO3_CHANNEL 8
#define SERVO4_CHANNEL 9
#define RSL_CHANNEL 10
// PWM Configuration
#define MOTOR_PWM_RES 10 // bits
#define MOTOR_PWM_FREQ 39000 // Based on AF Motor Shield, uses 39kHz for DC Motors
#define SERVO_PWM_RES 16 // bits
#define SERVO_PWM_FREQ 50 // Hz
#define RSL_PWM_RES 10 // bits
#define RSL_PWM_FREQ 1000 // Hz
// Motor states
#define FORWARD 1
#define BACKWARD 2
#define BRAKE 3
#define RELEASE 4
// RSL states
#define RSL_OFF 0
#define RSL_ON 1
#define RSL_DISABLED 2
#define RSL_ENABLED 3
// Drivetrain configurations
#define TWO_MOTORS 0
#define FOUR_MOTORS 1
class NoU_Motor {
public:
NoU_Motor(uint8_t motorPort);
void set(float output);
void setState(uint8_t state);
void setPower(uint16_t power);
void setInverted(boolean isInverted);
boolean isInverted();
void setMinimumOutput(float minimumOutput);
void setMaximumOutput(float maximumOutput);
void setExponent(float exponent);
void setDeadband(float deadband);
float getOutput();
private:
float applyCurve(float output);
uint8_t aPin;
uint8_t bPin;
uint8_t channel;
boolean inverted;
float output;
uint8_t state;
float minimumOutput = 0;
float maximumOutput = 1;
float exponent = 1;
float deadband = 0;
};
class NoU_Servo {
public:
NoU_Servo(uint8_t servoPort, uint16_t minPulse = 540, uint16_t maxPulse = 2300);
void write(float degrees);
void writeMicroseconds(uint16_t pulseLength);
void setMinimumPulse(uint16_t minPulse);
void setMaximumPulse(uint16_t maxPulse);
uint16_t getMicroseconds();
float getDegrees();
private:
uint8_t pin;
uint8_t channel;
uint16_t minPulse;
uint16_t maxPulse;
uint16_t pulse;
};
class NoU_Drivetrain {
public:
NoU_Drivetrain(NoU_Motor* leftMotor, NoU_Motor* rightMotor);
NoU_Drivetrain(NoU_Motor* frontLeftMotor, NoU_Motor* frontRightMotor,
NoU_Motor* rearLeftMotor, NoU_Motor* rearRightMotor);
void tankDrive(float leftPower, float rightPower);
void arcadeDrive(float throttle, float rotation, boolean invertedReverse = false);
void curvatureDrive(float throttle, float rotation, boolean isQuickTurn = true);
void holonomicDrive(float xVelocity, float yVelocity, float rotation);
void setMinimumOutput(float minimumOutput);
void setMaximumOutput(float maximumOutput);
void setInputExponent(float inputExponent);
void setInputDeadband(float inputDeadband);
private:
void setMotors(float frontLeftPower, float frontRightPower, float rearLeftPower, float rearRightPower);
float applyInputCurve(float input);
float applyOutputCurve(float output);
NoU_Motor *frontLeftMotor;
NoU_Motor *frontRightMotor;
NoU_Motor *rearLeftMotor;
NoU_Motor *rearRightMotor;
uint8_t drivetrainType;
float quickStopThreshold = 0.2;
float quickStopAlpha = 0.1;
float quickStopAccumulator;
float minimumOutput = 0;
float maximumOutput = 1;
float inputExponent = 1;
float inputDeadband = 0;
};
class RSL {
public:
static void initialize();
static void setState(uint8_t state);
static void update();
private:
static uint8_t state;
};
#endif