-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrx4.ino
159 lines (143 loc) · 4.09 KB
/
trx4.ino
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
byte light = LOW;
byte gear = LOW;
unsigned long gear_last_activated = 0;
unsigned long gear_last_deactivated = 0;
unsigned long differential_last_active = 0;
byte differential_front_state = UNLOCKED;
byte differential_back_state = UNLOCKED;
bool light_toggle_lock = false;
bool differential_front_lock = false;
bool differential_back_lock = false;
void light_on() {
Serial.println("Light on");
digitalWrite(LIGHT_PIN, HIGH);
light = HIGH;
}
void light_off() {
Serial.println("Light off");
digitalWrite(LIGHT_PIN, LOW);
light = LOW;
}
void toogle_light() {
if (light == LOW)
light_on();
else {
light_off();
}
}
void set_high_gear() {
Serial.println("HIGH gear");
gear_servo.writeMicroseconds(GEAR_HIGH);
gear = HIGH;
}
void set_low_gear() {
Serial.println("LOW gear");
gear_servo.writeMicroseconds(GEAR_LOW);
gear = LOW;
}
void toggle_gear() {
if (gear == LOW) {
set_high_gear();
} else {
set_low_gear();
}
}
void set_front(byte state) {
int uS;
if (state == LOCKED) {
uS = DIFFERENTIAL_FRONT_LOCK;
} else if (state == UNLOCKED) {
uS = DIFFERENTIAL_FRONT_UNLOCK;
} else {
Serial.println("Warning got unkonwn state for front differential!");
return;
}
differential_front_state = state;
Serial.println("Front differential " + String(state == LOCKED ? "locked" : "unlocked") );
differential_front_servo.writeMicroseconds(uS);
}
void set_back(byte state) {
int uS;
if (state == LOCKED) {
uS = DIFFERENTIAL_BACK_LOCK;
} else if (state == UNLOCKED) {
uS = DIFFERENTIAL_BACK_UNLOCK;
} else {
Serial.println("Warning got unkonwn state for back differential!");
return;
}
differential_back_state = state;
Serial.println("Back differential " + String(state == LOCKED ? "locked" : "unlocked") );
differential_back_servo.writeMicroseconds(uS);
}
void toggle_front() {
set_front(!differential_front_state);
}
void toggle_back() {
set_back(!differential_back_state);
}
void differential(float pos) {
if (pos > DIFFERENTIAL_FRONT_BOUND && !differential_front_lock) {
toggle_front();
differential_front_lock = true;
} else if (pos < DIFFERENTIAL_BACK_BOUND && !differential_front_lock) {
toggle_back();
differential_front_lock = true;
} else if (pos < DIFFERENTIAL_FRONT_BOUND && pos > DIFFERENTIAL_BACK_BOUND) {
differential_front_lock = false;
}
}
void gear_light(float pos) {
now = millis();
unsigned long period = now - gear_last_activated;
if (pos > GEAR_LIGHT_UPPER_BOUND && !gear_last_activated) {
gear_last_activated = now;
log(DEBUG, "pos above upper bound and not last active");
} else if (pos > GEAR_LIGHT_UPPER_BOUND && gear_last_activated) {
log(DEBUG, "pos above upper bound and last active");
if (period > LIGHT_TIME && !light_toggle_lock) {
log(DEBUG, "period longer then light and not locked");
toogle_light();
light_toggle_lock = true;
}
}
else if (pos < GEAR_LIGHT_LOWER_BOUND && gear_last_activated) {
log(DEBUG, "pos below lower bound and last active");
unsigned long pause = now - gear_last_deactivated;
if (pause < DOUBLEE_CLICK_TIME) {
reset_state();
} else if (period > GEAR_TIME && period < LIGHT_TIME) {
toggle_gear();
}
gear_last_activated = 0;
gear_last_deactivated = now;
light_toggle_lock = false;
}
}
void winch(float pos) {
if (pos > 0.5) {
digitalWrite(WINCH_IN_PIN, LOW);
digitalWrite(WINCH_OUT_PIN, HIGH);
Serial.println("winch OUT");
} else if (pos < -0.5) {
digitalWrite(WINCH_IN_PIN, HIGH);
digitalWrite(WINCH_OUT_PIN, LOW);
Serial.println("winch IN");
} else {
digitalWrite(WINCH_IN_PIN, LOW);
digitalWrite(WINCH_OUT_PIN, LOW);
}
}
void evaluate(int CH, float pos) {
log(DEBUG, "Channel: " + String(CH) + " | " + String(pos));
if (CH == DIFFERENTIAL_CH) {
log(DEBUG, "Channel: " + String(CH) + " Differential");
differential(pos);
} else if (CH == GEAR_LIGHT_CH) {
log(DEBUG, "Channel: " + String(CH) + " gear & light");
gear_light(pos);
} else if (CH == WINCH_CH) {
log(DEBUG, "Channel: " + String(CH) + " winch");
winch(pos);
}
}