This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrab_drive_modification.c
217 lines (165 loc) · 6.3 KB
/
crab_drive_modification.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
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
* FILE NAME: crab_drive_modification.c
*
* DESCRIPTION:
*
* This file contains code work by Rob Harris during the 2008 season.
*
*
Version History:
1/12/08 2:15 pm Start of Version History for this file
-Gyro Test function without Kevin Code
-Gyro startup funciton for Kevin's Code
-COMPLETE: Crab drive reduction function
1/26/08
-COMPLETE: General Rotary Dial function
*******************************************************************************/
#define GYRO_DEBUG 0
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "ifi_aliases.h"
#include "ifi_default.h"
#include "ifi_utilities.h"
#include "user_routines.h"
#include "chopshop.h"
#include "crab_drive_modification.h"
/***********************************************************************
Created by: Robert Harris - General coding and debugging
Per Hamnqvist - Mentor
Season: 2008
Function name: crab_drive_reduction
Parameters: unsigned char unreduced_drive_value = final number from the crab drive logic
unsigned char reduction = small number to be added to the motors which are driving optimally and subtracted from the
motors that are driving in the suboptimal direction
Returns: value adjusted for inconsistency of motor output in both directions
Purpose: The drive motors have an output dead zone that is greater in one direction than the other. This function adjusts the
final value from crab drive to recenter the dead zone at 127.
**********************************************************************/
unsigned char crab_drive_reduction(unsigned char unreduced_drive_value, unsigned char reduction)
{
unsigned char reduced_drive_value; // output hold value
if((unreduced_drive_value >= (127 - reduction)) && (unreduced_drive_value <= (127 + reduction))) // if within less of the reduction from 127
{
return 127; //return neutral
}
else if(unreduced_drive_value < (127 - reduction)) // if backwards
{
reduced_drive_value = unreduced_drive_value + reduction; // adjust dead zone to be closer to 127
}
else if((unreduced_drive_value > (127 + reduction)) && (unreduced_drive_value <= (254 - reduction))) // if forwards
{
reduced_drive_value = unreduced_drive_value + reduction; // adjust dead zone to be farther from 127 and low enough to prevent overflow
}
else if(unreduced_drive_value > (254 - reduction)) // if addition of reduction value would create overflow
{
reduced_drive_value = 254; // just set the value to the maximum
}
return reduced_drive_value; // return hold value
}
/***********************************************************************
Created by: Robert Harris - General coding and debugging
Season: 2008
Function name: drive_motor_compensation
Parameters: unsigned char pwm_input = The pwm drive input
unsigned char reduction_input = numbers from 0 and up from a rotary dial
unsigned char percentage_points_per_reduction_unit = number of percentage points per rotary dial click to reduce a drive motor's output
Returns: value adjusted for inherent inconsistency of motors and victors
Purpose: Every drive motor and victor is a little different. To make some drive functions work, it is necessary to reduce all motors to be equal to the
poorest performing motor-victor combination.
**********************************************************************/
unsigned char drive_motor_compensation(unsigned char drive_velocity_input, unsigned char reduction_input, unsigned char percentage_points_per_reduction_unit)
{
int hold_data; // integer value needed for reduction equations
if((drive_velocity_input >= (127 - DEAD_ZONE)) && (drive_velocity_input <= (127 + DEAD_ZONE))) // if in dead zone
{
return 127; // return neutral
}
else if (drive_velocity_input < 127)
{
hold_data = -1 * drive_velocity_input + 127; // make reversed -127 to 127 value
hold_data = hold_data * (100 - (reduction_input * percentage_points_per_reduction_unit)) * -1; // multiply by percentage and then unreverse
hold_data /= 100; // divide by 100 to make -127 to 127 value
hold_data += 127; // return to 0-254 value
return hold_data;
}
else if (drive_velocity_input > 127)
{
hold_data = drive_velocity_input - 127; // make -127 to 127 value
hold_data = hold_data * (100 - (reduction_input * percentage_points_per_reduction_unit)); // multiply by percentage
hold_data /= 100; // divide by 100 to return to -127 to 127 value
hold_data += 127; // return to 0-254 value
return hold_data;
}
}
unsigned char rotary_dial_2_function(void)
{
unsigned int rotary_voltage;
unsigned char rotary_input;
rotary_voltage = Get_Analog_Value(rotary_dial_2);
if(rotary_voltage < 50)
{
rotary_input = 0;
}
else if(rotary_voltage < 254 && rotary_voltage > 154)
{
rotary_input = 1;
}
else if(rotary_voltage < 459 && rotary_voltage > 359)
{
rotary_input = 2;
}
else if(rotary_voltage < 663 && rotary_voltage > 563)
{
rotary_input = 3;
}
else if(rotary_voltage < 868 && rotary_voltage > 767)
{
rotary_input = 4;
}
else if(rotary_voltage > 971)
{
rotary_input = 5;
}
return rotary_input;
}
/***********************************************************************
Created by: Robert Harris - General coding and debugging
Nick Plante - Original structure and design
Season: 2008
Function name: rotary_dial_general_output
Parameters: int rotary_dial = alias for the analog input that the rotary dial is plugged into
Returns: unsigned char from 0-5 indicating rotary dial position
Purpose: This function maps a rotary dial's six buttons to six distinct integer values
**********************************************************************/
unsigned char rotary_dial_general_output(int rotary_dial)
{
unsigned int rotary_voltage;
unsigned char rotary_input;
rotary_voltage = Get_Analog_Value(rotary_dial);
if(rotary_voltage < 50)
{
rotary_input = 0;
}
else if(rotary_voltage < 257 && rotary_voltage > 154)
{
rotary_input = 1;
}
else if(rotary_voltage < 462 && rotary_voltage > 359)
{
rotary_input = 2;
}
else if(rotary_voltage < 668 && rotary_voltage > 563)
{
rotary_input = 3;
}
else if(rotary_voltage < 873 && rotary_voltage > 767)
{
rotary_input = 4;
}
else if(rotary_voltage > 971)
{
rotary_input = 5;
}
return rotary_input;
}