-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadcell.h
198 lines (162 loc) · 4.6 KB
/
loadcell.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
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
#include "Arduino.h"
#include "WString.h"
#pragma once
#include "settings.h"
#include "init.h"
#include "leds.h"
#include "MyADS.h"
int CorrectedAxis1;
int CorrectedAxis2;
int MID_X = 13500;
int MID_Y = 13500;
int MinDeadZone = 800;
int LIMIT_X[3] = {15,13300 , 13450 };
int LIMIT_Y[3] = {15,13300 , 13450 };
int MAXVALUE=27000;
int CENTERVALUE = 13500;
int MINVALUE=0;
int VALUE_X;
int OLD_VALUE_X =0;
int VALUE_Y;
int OLD_VALUE_Y = 0;
int Axis1;
int Axis2;
int DeadZone = 25;
int EndZones = 160;
int InputLimit_Upper = MAXVALUE - EndZones ;
int InputLimit_Lower = MINVALUE + EndZones ;
int Center_Axis1 = 0;
int Center_Axis2 = 0;
long CalCyclesNum = 50;
long CalCycleEnd = 0;
long SumA = 0;
long SumB = 0;
int RepeatedCal = 0;
int InvertData(int Given){
return MAXVALUE - Given;
}
int ReadAxis(int Num){
if(Num == 1){
return (int)val0[0];
// return InvertData(analogRead(A1));
}
if(Num == 2){
return (int)val0[1];
// return InvertData(analogRead(A0));
}
}
int ClearEndZone(int rawvalue){
if (rawvalue > InputLimit_Upper) rawvalue = InputLimit_Upper;
if(rawvalue < InputLimit_Lower) rawvalue = InputLimit_Lower;
return rawvalue;
}
int GetCorrectedValue(int rawvalue, int center){
rawvalue = ClearEndZone(rawvalue);
int Output = CENTERVALUE;
int CalcValue = 0;
int RWD = abs( rawvalue - center );
if (RWD > DeadZone){
if(rawvalue < center - DeadZone){
CalcValue = map(rawvalue, InputLimit_Lower, center - DeadZone ,InputLimit_Lower,CENTERVALUE - 1);
// rohwert von 0+160 bis 13500-160 --> auf 0+160 bis 13499
Output = map(rawvalue,InputLimit_Lower,center - DeadZone,MINVALUE,CENTERVALUE - 1);
// calc von 0+160 bis 13500 auf 0 bis 13499
}
if(rawvalue > center + DeadZone){
CalcValue = map(rawvalue, center + DeadZone,InputLimit_Upper,CENTERVALUE + 1 ,InputLimit_Upper);
Output = map(rawvalue,center + DeadZone,InputLimit_Upper,CENTERVALUE + 1,MAXVALUE);
}
}
return Output;
}
void CalcluateLEDState(){
int DiffX = abs(MID_X - CorrectedAxis1);
int DiffY = abs(MID_Y - CorrectedAxis2);
LEDSTATE = 0;
int XState = 0;
int YState = 0;
for(int i = 0; i < 3 ; i++){
if (DiffX > LIMIT_X[i]) XState = i + 1;
if (DiffY > LIMIT_Y[i]) YState = i + 1;
}
LEDSTATE = max(YState,XState);
if(LEDSTATE == 1){
int mdiff = max(DiffX,DiffY);
SetGreen = map(mdiff,0,512,0,255);
int rdiff = 512 - mdiff;
SetRed = map(mdiff,0,512,0,25);
SetBlue = map(mdiff,0,512,0,25);
}
}
void ReadLoadCell(){
VALUE_X = ReadAxis(1);
VALUE_Y = ReadAxis(2);
int Changed = 0;
if(VALUE_X != OLD_VALUE_X){
CorrectedAxis1 = GetCorrectedValue(VALUE_X, Center_Axis1);
OLD_VALUE_X = VALUE_X;
Changed++;
}
if(VALUE_Y != OLD_VALUE_Y){
CorrectedAxis2 = GetCorrectedValue(VALUE_Y, Center_Axis2);
OLD_VALUE_Y = VALUE_Y;
Changed++;
}
if(Changed >0) CalcluateLEDState();
}
void GetCenter(){
SumA = 0;
SumB = 0;
long tmp = 0;
int CalDiff1 = 0;
int CalDiff2 = 0;
long NewDeadZone = 0;
long cnt = 0;
long oldCC = ADSCycles;
long minA = 32000;
long minB = 32000;
long maxA = 0;
long maxB = 0;
SetGreen = 255;
SetRed = 255;
SetBlue = 255;
LEDSTATE = 1;
ApplyState();
while(Center_Axis1 < 1){
MyAdsloop();
debug(".");
if(ADSCycles != oldCC){
debugln("");
cnt++;
Serial.println("New Analog values");
SumA = SumA + (long)val0[0];
if((long)val0[0] > maxA) maxA = (long)val0[0];
if((long)val0[0] < minA) minA = (long)val0[0];
SumB = SumB + (long)val0[1];
if((long)val0[1] > maxB) maxB = (long)val0[1];
if((long)val0[1] < minB) minB = (long)val0[1];
oldCC = ADSCycles;
debugln("Calcycle: " + (String)cnt + " Sums: " + (String)SumA + " / " + (String)SumB);
}
if(cnt == CalCyclesNum){
tmp = SumA / CalCyclesNum;
Center_Axis1 = (int) tmp;
debugln("Center-Run Axis1: Sum:" + (String)SumA + " in " + (String)CalCyclesNum +" runs");
tmp = SumB / CalCyclesNum;
Center_Axis2 = (int) tmp;
debugln("Center-Run Axis1: Sum:" + (String)SumB + " in " + (String)CalCyclesNum +" runs");
long t1 = 0;
long t2 = 0;
t1 = maxA - minA;
t2 = maxB - minB;
if(RepeatedCal > 0){
DeadZone = max((int)t1, (int)t2) * 2;
if(DeadZone < MinDeadZone) DeadZone = MinDeadZone;
debugln("New deadzone :" + (String)DeadZone);
}else{
DeadZone = MinDeadZone;
}
RepeatedCal++;
}
}
}