Skip to content

Commit 04c8f31

Browse files
committedNov 22, 2023
First pass at full animatronic movement
1 parent fbb387f commit 04c8f31

File tree

1 file changed

+95
-7
lines changed

1 file changed

+95
-7
lines changed
 

‎wsce_display_bot.ino

+95-7
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,20 @@ void rainbow(const double phase, Adafruit_NeoPixel *leds)
4646
for (int i = 0; i < leds->numPixels(); i++)
4747
{
4848
// 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+
}
5155

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
5261
// scale and set hue
53-
long pixel_hue = 65535 * hue_scale;
62+
long pixel_hue = 65535 * fractional(phase);
5463
leds->setPixelColor(i, leds->gamma32(leds->ColorHSV(pixel_hue)));
5564
}
5665
leds->show();
@@ -65,6 +74,7 @@ void pulse(const double phase, Adafruit_NeoPixel *leds, const unsigned char r, c
6574
}
6675
leds->show();
6776
}
77+
6878
void stripes(const double phase, Adafruit_NeoPixel *leds, const unsigned char r, const unsigned char g, unsigned char b)
6979
{
7080
const int strip_width = 4;
@@ -88,6 +98,7 @@ void stripes(const double phase, Adafruit_NeoPixel *leds, const unsigned char r,
8898
}
8999
leds->show();
90100
}
101+
91102
void solid(Adafruit_NeoPixel *leds, const unsigned char r, const unsigned char g, const unsigned char b)
92103
{
93104
const auto n = leds->numPixels();
@@ -110,30 +121,107 @@ void tick_motion(const double phase)
110121
{
111122
if (phase < 0.05)
112123
{
124+
const auto segment_phase = (phase - 0.0) / 0.05;
113125
// Pulse all lights as warning
114126
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);
118130
}
119131
else if (phase < 0.15)
120132
{
133+
// Intaking
121134
const auto segment_phase = (phase - 0.05) / 0.1;
122135
// Intake on
123136
stripes(fractional(segment_phase), intake_leds, 0, 255, 0);
124137
pulse(fractional(segment_phase * 5), shooter_leds, 255, 0, 0);
138+
solid_rainbow(fractional(phase * 2), chassis_leds);
125139
// Ramp up...
126140
const double duty = min(segment_phase * 2, 0.5);
127141
intake_motor.writeMicroseconds(throttle(duty));
128142
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+
}
129154
}
130155
else if (phase < 0.25)
131156
{
157+
// "Tracking" target
132158
intake_motor.writeMicroseconds(throttle(0));
133159
shooter_motor.writeMicroseconds(throttle(0));
134160
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));
135203

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);
137225
}
138226
}
139227

0 commit comments

Comments
 (0)