@@ -46,11 +46,20 @@ void rainbow(const double phase, Adafruit_NeoPixel *leds)
46
46
for (int i = 0 ; i < leds->numPixels (); i++)
47
47
{
48
48
// remap phased hue to be within 0 and 1
49
- double hue_scale = (i * resolution + phase);
50
- hue_scale = hue_scale - (int )hue_scale;
49
+ // scale and set hue
50
+ long pixel_hue = 65535 * fractional (i * resolution + phase);
51
+ leds->setPixelColor (i, leds->gamma32 (leds->ColorHSV (pixel_hue)));
52
+ }
53
+ leds->show ();
54
+ }
51
55
56
+ void solid_rainbow (const double phase, Adafruit_NeoPixel *leds)
57
+ {
58
+ for (int i = 0 ; i < leds->numPixels (); i++)
59
+ {
60
+ // remap phased hue to be within 0 and 1
52
61
// scale and set hue
53
- long pixel_hue = 65535 * hue_scale ;
62
+ long pixel_hue = 65535 * fractional (phase) ;
54
63
leds->setPixelColor (i, leds->gamma32 (leds->ColorHSV (pixel_hue)));
55
64
}
56
65
leds->show ();
@@ -65,6 +74,7 @@ void pulse(const double phase, Adafruit_NeoPixel *leds, const unsigned char r, c
65
74
}
66
75
leds->show ();
67
76
}
77
+
68
78
void stripes (const double phase, Adafruit_NeoPixel *leds, const unsigned char r, const unsigned char g, unsigned char b)
69
79
{
70
80
const int strip_width = 4 ;
@@ -88,6 +98,7 @@ void stripes(const double phase, Adafruit_NeoPixel *leds, const unsigned char r,
88
98
}
89
99
leds->show ();
90
100
}
101
+
91
102
void solid (Adafruit_NeoPixel *leds, const unsigned char r, const unsigned char g, const unsigned char b)
92
103
{
93
104
const auto n = leds->numPixels ();
@@ -110,30 +121,107 @@ void tick_motion(const double phase)
110
121
{
111
122
if (phase < 0.05 )
112
123
{
124
+ const auto segment_phase = (phase - 0.0 ) / 0.05 ;
113
125
// Pulse all lights as warning
114
126
stop_all_motors ();
115
- pulse (fractional (phase / 0.05 * 5 ), intake_leds, 255 , 0 , 0 );
116
- solid ( shooter_leds, 0 , 0 , 0 );
117
- solid ( chassis_leds, 100 , 100 , 100 );
127
+ pulse (fractional (segment_phase * 5 ), intake_leds, 255 , 0 , 0 );
128
+ pulse ( fractional (segment_phase * 2 ), shooter_leds, 0 , 0 , 255 );
129
+ pulse ( fractional (segment_phase * 2 ), chassis_leds, 0 , 0 , 255 );
118
130
}
119
131
else if (phase < 0.15 )
120
132
{
133
+ // Intaking
121
134
const auto segment_phase = (phase - 0.05 ) / 0.1 ;
122
135
// Intake on
123
136
stripes (fractional (segment_phase), intake_leds, 0 , 255 , 0 );
124
137
pulse (fractional (segment_phase * 5 ), shooter_leds, 255 , 0 , 0 );
138
+ solid_rainbow (fractional (phase * 2 ), chassis_leds);
125
139
// Ramp up...
126
140
const double duty = min (segment_phase * 2 , 0.5 );
127
141
intake_motor.writeMicroseconds (throttle (duty));
128
142
shooter_motor.writeMicroseconds (throttle (-0.1 ));
143
+
144
+ // Bring tilt down if not already
145
+ if (digitalRead (TILT_LOWER_LIMIT_PIN))
146
+ {
147
+ // Limit switches are high until pressed
148
+ tilt_motor.writeMicroseconds (throttle (-0.1 ));
149
+ }
150
+ else
151
+ {
152
+ tilt_motor.writeMicroseconds (throttle (0 ));
153
+ }
129
154
}
130
155
else if (phase < 0.25 )
131
156
{
157
+ // "Tracking" target
132
158
intake_motor.writeMicroseconds (throttle (0 ));
133
159
shooter_motor.writeMicroseconds (throttle (0 ));
134
160
solid (intake_leds, 0 , 0 , 0 );
161
+ solid_rainbow (fractional (phase * 2 ), chassis_leds);
162
+ }
163
+ else if (phase < 0.85 )
164
+ {
165
+ const auto segment_phase = (phase - 0.25 ) / 0.6 ;
166
+ // "tracking"
167
+ pulse (fractional (segment_phase * 5 ), shooter_leds, 255 , 255 , 128 ); // slow pulse yellow
168
+
169
+ // two cycles of sin curve to get up and down
170
+ double desired_throttle = sin (2.0 * 3.14159 * segment_phase * 2 );
171
+ // apply decay to make the amplitude decrease with time
172
+ desired_throttle *= pow (2.0 , -segment_phase * 5.0 );
173
+
174
+ // Check we haven't hit a limit switch
175
+ if (digitalRead (TILT_LOWER_LIMIT_PIN))
176
+ {
177
+ // Limit switches are high until pressed
178
+ desired_throttle = max (desired_throttle, 0.0 );
179
+ }
180
+ if (digitalRead (TILT_UPPER_LIMIT_PIN))
181
+ {
182
+ // Limit switches are high until pressed
183
+ desired_throttle = min (desired_throttle, 0.0 );
184
+ }
185
+ tilt_motor.writeMicroseconds (throttle (desired_throttle));
186
+
187
+ solid_rainbow (fractional (phase * 2 ), chassis_leds);
188
+ }
189
+ else if (phase < 0.90 )
190
+ {
191
+ const auto segment_phase = (phase - 0.85 ) / 0.05 ;
192
+ // Shooter warning
193
+ pulse (fractional (segment_phase * 5 ), shooter_leds, 255 , 0 , 0 );
194
+
195
+ solid_rainbow (fractional (phase * 2 ), chassis_leds);
196
+ }
197
+ else if (phase < 0.95 )
198
+ {
199
+ const auto segment_phase = (phase - 0.90 ) / 0.05 ;
200
+ // Shooter fire
201
+ pulse (fractional (segment_phase * 10 ), shooter_leds, 255 , 0 , 0 );
202
+ shooter_motor.writeMicroseconds (throttle (segment_phase));
135
203
136
- // More stuff...
204
+ solid_rainbow (fractional (phase * 2 ), chassis_leds);
205
+ }
206
+ else
207
+ {
208
+ const auto segment_phase = (phase - 0.95 ) / 0.05 ;
209
+ // Green pulse to show we are done
210
+ shooter_motor.writeMicroseconds (throttle (0 ));
211
+
212
+ // Bring tilt down
213
+ if (digitalRead (TILT_LOWER_LIMIT_PIN))
214
+ {
215
+ // Limit switches are high until pressed
216
+ tilt_motor.writeMicroseconds (throttle (-0.1 ));
217
+ }
218
+ else
219
+ {
220
+ tilt_motor.writeMicroseconds (throttle (0 ));
221
+ }
222
+ pulse (fractional (segment_phase * 5 ), intake_leds, 0 , 255 , 0 );
223
+ pulse (fractional (segment_phase * 2 ), shooter_leds, 0 , 255 , 0 );
224
+ pulse (fractional (segment_phase * 2 ), chassis_leds, 0 , 255 , 0 );
137
225
}
138
226
}
139
227
0 commit comments