-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLordSwerve.c
207 lines (162 loc) · 4.38 KB
/
LordSwerve.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef LordSwerve_h
#define LordSwerve_h
#include "FTC_PID.c"
//#include "FTC_Gyro.c"
#include "drivers/HTSPB-driver.h"
typedef struct
{
PID turnPID;
int driveSpeed;
TServoIndex turnMotor;
int turnZeroOffset;
tMotor driveMotor;
bool inverted;
int pos;
bool driveMotorInverted;
int number;
} SwerveModule;
/*
float atan2(float x, float y)
{
float a;
if (x>0) a = atan(y/x);
if (y>=0 && x<0) a = atan(y/x) + PI;
if (y<0 && x<0) a = atan(y/x) - PI;
if (y>0 && x==0) a = PI/2.0;
if (y<0 && x==0) a =-PI/2.0;
if (x==0 && y==0) a = 0;
return a;
}*/
void initSwerveModule(int number, tMotor driveMotor, TServoIndex turnMotor, bool inverted = false, int turnZeroOffset);
void updateSwerveModule(SwerveModule &swerveModule);
SwerveModule modules[4];
/* Swerve Drive */
void initSwerveDrive()
{
initSwerveModule(0, 1, pod0Steer, false, -5);
initSwerveModule(1, 2, pod1Steer, false, 0);
initSwerveModule(2, 3, pod2Steer, true, 0);
initSwerveModule(3, 4, pod3Steer, false, 0);
servo[pod0Steer] = 127;
servo[pod1Steer] = 127;
servo[pod2Steer] = 127;
servo[pod3Steer] = 127;
}
void updateSwerveDrive()
{
updateSwerveModule(modules[0]);
}
/* Swerve module library */
void initSwerveModule(int number, tMotor driveMotor, TServoIndex turnMotor, bool inverted, int turnZeroOffset)
{
initPID(modules[number].turnPID, 0.01, 0, 0);
modules[number].turnPID.target = 200;
modules[number].turnMotor = turnMotor;
modules[number].driveMotor = driveMotor;
modules[number].number = number;
modules[number].inverted = inverted;
modules[number].turnZeroOffset = turnZeroOffset;
int reading = HTSPBreadADC(proto, number, 8);
}
void setModuleTarget(int module, int target)
{
if ( modules[module].inverted )
target = 255-target;
modules[module].turnPID.target = target;
}
void setRawModuleTarget(int module, int target)
{
modules[module].turnPID.target = target;
}
void setModuleSpeed(int module, int target)
{
modules[module].driveSpeed = target;
}
bool moduleAtTarget(int module)
{
return abs(modules[module].turnPID.error) < 180;
}
void updateSwerveModule(SwerveModule &swerveModule)
{
int reading = HTSPBreadADC(proto, swerveModule.number, 8);
int turnSpeed = calcPID(swerveModule.turnPID, reading);
if ( swerveModule.inverted )
turnSpeed = -turnSpeed;
servo[swerveModule.turnMotor] = -(turnSpeed+(swerveModule.turnZeroOffset+127));
nxtDisplayString(5, "%i", -(turnSpeed+(swerveModule.turnZeroOffset+127)));
/*
if ( abs(swerveModule.driveSpeed) > 10 )
motor[swerveModule.driveMotor] = (swerveModule.driveMotorInverted?-swerveModule.driveSpeed:swerveModule.driveSpeed);
else if ( turnSpeed > 10 )
motor[swerveModule.driveMotor] = -13;
else if ( turnSpeed < -10 )
motor[swerveModule.driveMotor] = 13;
else
motor[swerveModule.driveMotor] = 0;
*/
}
/* Drive Modes */
/*
void fieldCentricCrab()
{
int magnitude = sqrt(pow(joystick.joy1_y1, 2)+pow(joystick.joy1_x1, 2));
int angle = radiansToDegrees(atan2(joystick.joy1_x1,joystick.joy1_y1));
int target = (int)floor(1.41666666*angle);
if (magnitude > 30 )
magnitude = 30;
if ( joystick.joy1_y1 < 0 )
magnitude = -magnitude;
if ( !(moduleAtTarget(0)&&moduleAtTarget(1)&&moduleAtTarget(2)&&moduleAtTarget(3)) )
magnitude = 0;
setModuleTarget(0, target);
/* for ( int i = 0; i < 4; i++ )
{
//setModuleSpeed(i, magnitude);
//nxtDisplayString(i, "%i - %i", i, HTSPBreadADC(proto, i, 10));
}
}
void chassisRotation()
{
int magnitude = joystick.joy1_x2;
// Turn diags together
setRawModuleTarget(0, 212);
setRawModuleTarget(2, 311);
setRawModuleTarget(1, 231);
setRawModuleTarget(3, 276);
if ( !(moduleAtTarget(0)&&moduleAtTarget(1)&&moduleAtTarget(2)&&moduleAtTarget(3)) )
magnitude = 0;
for ( int i = 0; i < 4; i++ )
{
setModuleSpeed(i, magnitude);
}
}
*/
/*
void snakeDrive()
{
float insideA = 0;
float outsideA = 0;
static float l = 18;
static float w = 18;
float Acl = 0;
Acl = toRadians(10);
float Rcl = l/(2*sin(Acl));
float Rcp = (l/2)/tan(Acl);
outsideA = atan((l/2)/(Rcp+(w/2)));
if ( Rcp == w/2 )
{
insideA = PI/2;
}
else if ( Rcp > w/2 )
{
insideA = atan((l/2)/(Rcp-w/2));
}
else if ( Rcp < w/2 )
{
insideA = PI-atan((l/2)/(Rcp-w/2));
}
outsideA = radiansToDegrees(outsideA);
insideA = radiansToDegrees(insideA);
}
*/
#endif