Commit ae79f333 authored by Vidisha Srivastav's avatar Vidisha Srivastav
Browse files

added dummy load check + enable

created integrated LED control + motor control script
parent 89fdb649
......@@ -26,6 +26,8 @@
#define OFFSET_2 45
#define OFFSET_3 70
uint8_t off[3];
static void led_gradient (uint8_t grb_array[], uint8_t color1[], uint8_t color2[], uint8_t start, uint8_t end, uint8_t size) {
// grb_array is an instantiated array of size num_leds*3 with structure
......@@ -87,26 +89,40 @@ void shared_module_ee285_body_leds(mp_obj_t rgb)
uint8_t pixels[NUM_BODY_LEDS*3];
//Set all GRB values to the input
for (int i = 0; i < NUM_BODY_LEDS; i++) //This doesn't modify the rgb values
{
pixels[3*i + 0] = GRB[0];
pixels[3*i + 1] = GRB[1];
pixels[3*i + 2] = GRB[2];
}
mcu_pin_obj_t pin;
pin.number = PIN_PA04; //A4 on m4 express, see ports/atmel-samd/boards/[board]/pins.c for other pins
//Set up the pin
gpio_set_pin_direction(pin.number, GPIO_DIRECTION_OUT);
gpio_set_pin_pull_mode(pin.number, GPIO_PULL_OFF);
digitalio_digitalinout_obj_t io;
io.pin = &pin;
// Check that leds will be turned on
off[2] = (GRB[0] || GRB[1] || GRB[2]) ? 0 : 1;
// Sets dummy load pin to HIGH if all LEDs (wing + body) will be off
if(off[0] && off[1] && off[2]){
pin.number = PIN_PB09; // Pin A3 on Feather M4 Express. project pinout for LED dummy load
gpio_set_pin_direction(pin.number, GPIO_DIRECTION_OUT);
gpio_set_pin_level(pin.number, 1);
}
else{
//Set all GRB values to the input
for (int i = 0; i < NUM_BODY_LEDS; i++) //This doesn't modify the rgb values
{
pixels[3*i + 0] = GRB[0];
pixels[3*i + 1] = GRB[1];
pixels[3*i + 2] = GRB[2];
}
pin.number = PIN_PA21; // Pin 11 on Feather M4 Express
// pin.number = PIN_PA04; // A4 on m4 express, see ports/atmel-samd/boards/[board]/pins.c for other pins
//Set up the pin
gpio_set_pin_direction(pin.number, GPIO_DIRECTION_OUT);
gpio_set_pin_pull_mode(pin.number, GPIO_PULL_OFF);
digitalio_digitalinout_obj_t io;
io.pin = &pin;
//Output the array to the LEDs,
//this turns off interrupts then turns them back on after it finishes
common_hal_neopixel_write(&io, pixels, NUM_BODY_LEDS*3);
//Output the array to the LEDs,
//this turns off interrupts then turns them back on after it finishes
common_hal_neopixel_write(&io, pixels, NUM_BODY_LEDS*3);
}
}
......@@ -120,53 +136,76 @@ void shared_module_ee285_wing_leds(int side, mp_obj_t rgb1, mp_obj_t rgb2, mp_ob
{
mp_raise_ValueError_varg(translate("Expected tuple of length %3, got %d"), len);
}
uint8_t GRB1[3];
GRB1[0] = mp_obj_get_int(items[1]);
GRB1[1] = mp_obj_get_int(items[0]);
GRB1[2] = mp_obj_get_int(items[2]);
uint8_t GRB[3][3];
GRB[0][0] = mp_obj_get_int(items[1]);
GRB[0][1] = mp_obj_get_int(items[0]);
GRB[0][2] = mp_obj_get_int(items[2]);
mp_obj_get_array(rgb2, &len, &items);
if (len < 3 || len > 4)
{
mp_raise_ValueError_varg(translate("Expected tuple of length %3, got %d"), len);
}
uint8_t GRB2[3];
GRB2[0] = mp_obj_get_int(items[1]);
GRB2[1] = mp_obj_get_int(items[0]);
GRB2[2] = mp_obj_get_int(items[2]);
GRB[1][0] = mp_obj_get_int(items[1]);
GRB[1][1] = mp_obj_get_int(items[0]);
GRB[1][2] = mp_obj_get_int(items[2]);
mp_obj_get_array(rgb3, &len, &items);
if (len < 3 || len > 4)
{
mp_raise_ValueError_varg(translate("Expected tuple of length %3, got %d"), len);
}
uint8_t GRB3[3];
GRB3[0] = mp_obj_get_int(items[1]);
GRB3[1] = mp_obj_get_int(items[0]);
GRB3[2] = mp_obj_get_int(items[2]);
// TODO: Check if all inputs are 0, send
GRB[2][0] = mp_obj_get_int(items[1]);
GRB[2][1] = mp_obj_get_int(items[0]);
GRB[2][2] = mp_obj_get_int(items[2]);
uint8_t pixels[NUM_WING_LEDS*3];
//Offset 1 to 2
led_gradient (pixels, GRB1, GRB2, OFFSET_1*3, OFFSET_2*3, (NUM_WING_LEDS-1)*3);
//Offset 2 to 3
led_gradient (pixels, GRB2, GRB3, OFFSET_2*3, OFFSET_3*3, (NUM_WING_LEDS -1)*3);
//This interpolation wraps around by treating pixels as a bigger array than it is
led_gradient (pixels, GRB3, GRB1, OFFSET_3*3, (NUM_WING_LEDS + OFFSET_1)*3, (NUM_WING_LEDS)*3);
// Check that LEDs are all off
for(uint8_t i = 0; i < 3; i++){
for(uint8_t j = 0; j < 3; j++){
if(GRB[i][j]){
off[side] = 0;
break;
}
}
}
mcu_pin_obj_t pin;
pin.number = side == 1 ? PIN_PA02 : PIN_PB08; //A0 and A2 on the feather m4, look up in a pins.h for other boards
digitalio_digitalinout_obj_t io;
// Sets dummy load pin to HIGH if all LEDs (wing + body) will be off
if(off[0] && off[1] && off[2])
{
pin.number = PIN_PB09; // Pin A3 on Feather M4 Express. project pinout for LED dummy load
gpio_set_pin_direction(pin.number, GPIO_DIRECTION_OUT);
gpio_set_pin_level(pin.number, 1);
io.pin = &pin;
common_hal_neopixel_write(&io, pixels, NUM_WING_LEDS*3);
}
else
{
pin.number = side == 1 ? PIN_PA20 : PIN_PA22; // Pins 12 and 13 on the Feather M4 Express
// pin.number = side == 1 ? PIN_PA02 : PIN_PB08; //A0 and A2 on the feather m4, look up in a pins.h for other boards
//Offset 1 to 2
led_gradient (pixels, GRB[0], GRB[1], OFFSET_1*3, OFFSET_2*3, (NUM_WING_LEDS-1)*3);
//Offset 2 to 3
led_gradient (pixels, GRB[1], GRB[2], OFFSET_2*3, OFFSET_3*3, (NUM_WING_LEDS -1)*3);
//This interpolation wraps around by treating pixels as a bigger array than it is
led_gradient (pixels, GRB[2], GRB[0], OFFSET_3*3, (NUM_WING_LEDS + OFFSET_1)*3, (NUM_WING_LEDS)*3);
gpio_set_pin_direction(pin.number, GPIO_DIRECTION_OUT);
gpio_set_pin_pull_mode(pin.number, GPIO_PULL_OFF);
io.pin = &pin;
common_hal_neopixel_write(&io, pixels, NUM_WING_LEDS*3);
}
// TODO: if all inputs 0, set a pre-determined pin to OUTPUT and HIGH
gpio_set_pin_direction(pin.number, GPIO_DIRECTION_OUT);
gpio_set_pin_pull_mode(pin.number, GPIO_PULL_OFF);
digitalio_digitalinout_obj_t io;
io.pin = &pin;
common_hal_neopixel_write(&io, pixels, NUM_WING_LEDS*3);
}
static bool motor_control_initted = false;
......
# This script tests flapping the wings on a Fractal Flyer.
# It uses the motor control loop implemented in the firmware.
import time
import ee285
import board
import busio
import math
import digitalio
import microcontroller
import pulseio
LEDS_PER_WING = 180
LEDS_IN_BODY = 200
# ee285.motor_control_loop_init()
left_wing_high = 45 #flap left wing this many degrees above level
left_wing_low = -45 #flap left wing this many degrees below level
left_wing_target_angle = left_wing_high
period = 20 #Period (s) before reversing target angle
'''Cycles through colors (used for LED patterns)'''
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos*3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos*3)
g = 0
b = int(pos*3)
else:
pos -= 170
r = 0
g = int(pos*3)
b = int(255 - pos*3)
return (r, g, b)
pattern = []
for i in range(0, 512):
pattern.append(wheel(int(i/2) % 256))
counter = 0
start = (int)(time.monotonic())
last_passed = -1
while True:
passed = (int)(time.monotonic()) - start
#If desired, smoother operation could be achieved by only changing lights with wings stationary
#TODO: Any communication here (change behavior by modifying global variables)
if passed%period == 0 and passed != last_passed:
left_wing_target_angle = -left_wing_target_angle
print("Wings moving to angle", left_wing_target_angle)
last_passed = passed
# Issue: wings aren't flapping all the way up, as they should be.
# Set left and right wings to desired angle
# ee285.wing_angle(0, left_wing_target_angle)
# ee285.wing_angle(1, left_wing_target_angle)
#Pattern LEDS.
color1 = counter
color2_wing = (counter + int(LEDS_PER_WING/2)) % 512
color3 = (counter + LEDS_PER_WING) % 512
# print(pattern[color1])
# print("Color 1: ", color1, " Color 2: ", color2_wing, " Color 3: ", color3, "\nCounter: ", counter)
ee285.wing_leds(0,pattern[color1], pattern[color2_wing], pattern[color3])
ee285.wing_leds(1,pattern[color1], pattern[color2_wing], pattern[color3]) # flickers for some reason ??
# ee285.body_leds(pattern[color1:])
time.sleep(0.01)
counter = (counter+1) % 512
......@@ -3,22 +3,58 @@
import time
import ee285
import board
import busio
import math
import digitalio
import microcontroller
import pulseio
import neopixel
ee285.motor_control_loop_init()
LEDS_PER_WING = 180
LEDS_IN_BODY = 200
# ee285.motor_control_loop_init()
left_wing_high = 45 #flap left wing this many degrees above level
left_wing_low = -45 #flap left wing this many degrees below level
left_wing_target_angle = left_wing_high
period = 20 #Period (s) before reversing target angle
'''Cycles through colors (used for LED patterns)'''
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos*3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos*3)
g = 0
b = int(pos*3)
else:
pos -= 170
r = 0
g = int(pos*3)
b = int(255 - pos*3)
return (r, g, b)
pattern = []
for i in range(0, 512):
pattern.append(wheel(int(i/2) % 256))
counter = 0
start = (int)(time.monotonic())
last_passed = -1
while True:
passed = (int)(time.monotonic()) - start
#TODO: Pattern LEDS here. For now, simulate with delay
time.sleep(0.01)
#If desired, smoother operation could be achieved by only changing lights with wings stationary
#TODO: Any communication here (change behavior by modifying global variables)
......@@ -31,5 +67,15 @@ while True:
# Issue: wings aren't flapping all the way up, as they should be.
# Set left and right wings to desired angle
ee285.wing_angle(0, left_wing_target_angle)
ee285.wing_angle(1, left_wing_target_angle)
# ee285.wing_angle(0, left_wing_target_angle)
# ee285.wing_angle(1, left_wing_target_angle)
#Pattern LEDS.
# time.sleep(0.01)
color1 = counter
color2_wing = counter-LEDS_PER_WING/2
color3 = counter - LEDS_PER_WING
ee285.wing_leds(0,pattern[color1], pattern[color2], pattern[color3])
ee285.wing_leds(1,pattern[color1], pattern[color2], pattern[color3])
ee285.body_leds(pattern[counter:])
counter = (counter+1) % 256
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment