-
Notifications
You must be signed in to change notification settings - Fork 0
/
Globals.c
195 lines (184 loc) · 4.31 KB
/
Globals.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
#include <stdlib.h>
#include "Globals.h"
#include "10sk.h"
#include "MagAcc.h"
#include "PIDController.h"
int c_ticks;
int allowedTolerance;
int moveCount;
char key;
volatile int motorRunning;
int paused;
double old_value;
int useTilt=0;
int bailOutBadCompass=0;
int shaftPosition=MIDDLE;
const char deg_S[8] = {0x08,0x14,0x08,0x00,0x00,0x00,0x00,0x00};
// added object from compass.h
compass heading={0,0,0,0};
/*****************************************************************************
Name: getCompassReading
Parameters: none
Returns: a newly read compass heading from the digital compass
Description: returns a newly read compass heading from the digital compass
*****************************************************************************/
void setCompass(int compassReading){
heading.current=compassReading;
heading.tilt=tilt;
}
void setDesired(int userSelectedDegrees){
heading.desired=userSelectedDegrees;
heading.desiredTilt=tilt;
}
int getCompassReading(int avg){
double newDigitHeading;
double old_value;
//txs=0;
newDigitHeading=get_digital_heading();
//txs=1;
//old_value=heading.current;
newDigitHeading-=180;//flip the compass to correct digital compass position
if (newDigitHeading < 0) newDigitHeading += 360;//correct for 0 turns to 360
if(avg&°reeDifferance(1,newDigitHeading,heading.current)<=7){
if(abs(heading.current-newDigitHeading)>180){//for across 360 zero area
//required for averaging across 360 zero area
old_value=newDigitHeading+degreeDifferance(0,heading.current,newDigitHeading);
}
else{
old_value=heading.current;
}
newDigitHeading=((11.0/16.0)*old_value)+(((16.0-11.0)/16.0)*newDigitHeading);
if(newDigitHeading>360)
newDigitHeading-=360;
if(newDigitHeading<0){
newDigitHeading+=360;
}
bailOutBadCompass=0;
}
else if(avg){
newDigitHeading=(double)heading.current;
if(bailOutBadCompass++>3){
newDigitHeading=startCompass();
bailOutBadCompass=0;
}
}
return (int)newDigitHeading;
}
//write smoothing compass for menutask before follow starts
int startCompass(){
int i=0;
int newReading=0;
for(i=0;i<2;i++){
setCompass(getCompassReading(0));
}
for(i=0;i<3;i++){
setCompass(getCompassReading(1));
}
// PID.settleTime=5;//let the dterm and iterm get back on track before starting
return heading.current;
}
/* if the absoluteValue is set to 0 then degreeDifferance will have a sign pos or neg */
int degreeDifferance(int absoluteValue,int current,int desired){
int compDiff=0;
int accrossN=0;
compDiff=abs(current-desired);
if(compDiff>180){
compDiff=abs(compDiff-360);
accrossN=1;
}
if(absoluteValue || ((heading.current>heading.desired && accrossN==0) || (accrossN==1 && heading.current<heading.desired))){
return compDiff;
}
else{
return (-1*compDiff);
}
}
void toStr(char strNum[9],int value,int menuNum){
strNum[0]=' ';
strNum[1]=' ';
if(menuNum==8){
strNum[2]='>';
}
else{
strNum[2]=' ';
}
digits2String(value,3,strNum);
if(menuNum!=1 ){
strNum[6]=0x07;
}
else{
strNum[6]=' ';
}
if(menuNum==9){
strNum[7]='<';
}
else{
strNum[7]=' ';
}
strNum[8]='\0';
}
void digits2String(int value,int pos,char strNum[9]){
strNum[0+pos]=(value/100)+48;
strNum[1+pos]=(((value%100)/10))+48;
strNum[2+pos]=(value%10)+48;
}
void tiltToStr(char strNum[9],int value){
strNum[0]='T';
strNum[1]='I';
strNum[2]='L';
strNum[3]='T';
strNum[4]=' ';
digits2String(value,5,strNum);
strNum[8]='\0';
}
void numsToStr(char strNum[9],int value1,int value2){
digits2String(value1,0,strNum);
strNum[3]='<';
digits2String(value2,4,strNum);
strNum[7]=' ';
strNum[8]='\0';
}
void tack2Heading(int starboard){
motorRunning=0;
txs=0;
p4_1=0;//set move out to off
p4_0=0;//set move in to off
tdelay(50);
if(starboard==1){
motorRunning=1;
}
else{
motorRunning=-1;
}
PID.runTime=130;
txs=1;
setCompass(getCompassReading(1));
while(paused==0&°reeDifferance(1,heading.current,heading.desired)>35){
setCompass(getCompassReading(0));
check4ShaftNOut();
}
motorRunning=0;
txs=0;
p4_1=0;//set move out to off
p4_0=0;//set move in to off
tdelay(50);
}
void check4ShaftNOut(){
if(p4_2){
if(motorRunning<0){
motorRunning=0;
}
p4_0=0;
shaftPosition=ALLIN;
}
else if(p4_3){
if(motorRunning>0){
motorRunning=0;
}
p4_1=0;
shaftPosition=ALLOUT;
}
else{
shaftPosition=MIDDLE;
}
}