diff --git a/micropython/examples/servo2040/led_rainbow.py b/micropython/examples/servo2040/led_rainbow.py new file mode 100644 index 00000000..35a811a5 --- /dev/null +++ b/micropython/examples/servo2040/led_rainbow.py @@ -0,0 +1,36 @@ +import time +from pimoroni import Button +from plasma import WS2812 +from servo import servo2040 + +# Press "Boot" to exit the program. + +SPEED = 5 # The speed that the LEDs will cycle at +BRIGHTNESS = 0.4 # The brightness of the LEDs +UPDATES = 60 # How many times the LEDs will be updated per second + +# Create the LED bar +led_bar = WS2812(servo2040.NUM_LEDS, 1, 0, servo2040.LED_DAT) + +# Create the user button +user_sw = Button(servo2040.USER_SW) + +# Start updating the LED bar +led_bar.start() + + +offset = 0.0 + +# Make rainbows until the user button is pressed +while user_sw.raw() != True: + + offset += SPEED / 1000.0 + + for i in range(servo2040.NUM_LEDS): + hue = float(i) / servo2040.NUM_LEDS + led_bar.set_hsv(i, hue + offset, 1.0, BRIGHTNESS) + + time.sleep(1.0 / UPDATES) + +# Turn off the LED bar +led_bar.clear() \ No newline at end of file diff --git a/micropython/examples/servo2040/multiple_servos.py b/micropython/examples/servo2040/multiple_servos.py index bd70f375..61fc276d 100644 --- a/micropython/examples/servo2040/multiple_servos.py +++ b/micropython/examples/servo2040/multiple_servos.py @@ -1,10 +1,10 @@ import time import math -from servo import Servo +from servo import Servo, servo2040 # Create a list of servos for pins 0 to 3. Up to 16 servos can be created -START_PIN = 0 -END_PIN = 3 +START_PIN = servo2040.SERVO_1 +END_PIN = servo2040.SERVO_3 servos = [Servo(i) for i in range(START_PIN, END_PIN + 1)] # Enable all servos (this puts it at the middle) diff --git a/micropython/examples/servo2040/servo_cluster.py b/micropython/examples/servo2040/servo_cluster.py index a3017c0a..77398ee9 100644 --- a/micropython/examples/servo2040/servo_cluster.py +++ b/micropython/examples/servo2040/servo_cluster.py @@ -1,16 +1,15 @@ import time import math -from servo import ServoCluster +from servo import ServoCluster, servo2040 -# NOTE: ServoCluster lets you control up to 30 servos at once. -# This is done using the RP2040's PIO system. As such it is experimental -# and may have edge-cases that will need to be fixed. This is particularly -# true when attempting to run a program multiple times. -# If you do encounter issues, try resetting your board. +# NOTE: ServoCluster lets you control up to 30 servos at once. This is done using the +# RP2040's PIO system. As such it is experimental and may have edge-cases that will +# need to be fixed. This is particularly true when attempting to run a program multiple +# times. If you do encounter issues, try resetting your board. # Create a servo cluster for pins 0 to 3, using PIO 0 and State Machine 0 -START_PIN = 0 -END_PIN = 3 +START_PIN = servo2040.SERVO_1 +END_PIN = servo2040.SERVO_4 servos = ServoCluster(pio=0, sm=0, pins=list(range(START_PIN, END_PIN + 1))) # Enable all servos (this puts it at the middle) @@ -29,10 +28,10 @@ time.sleep(2) servos.all_to_mid() time.sleep(2) -SWEEPS = 3 # How many sweeps of the servo to perform -STEPS = 10 # The number of discrete sweep steps -STEPS_INTERVAL = 0.5 # The time in seconds between each step of the sequence -SWEEP_EXTENT = 90.0 # How far from zero to move the servo when sweeping +SWEEPS = 3 # How many sweeps of the servo to perform +STEPS = 10 # The number of discrete sweep steps +STEPS_INTERVAL = 0.5 # The time in seconds between each step of the sequence +SWEEP_EXTENT = 90.0 # How far from zero to move the servo when sweeping # Do a sine sweep for j in range(SWEEPS): diff --git a/micropython/examples/servo2040/single_servo.py b/micropython/examples/servo2040/single_servo.py index 95f2b943..7b6d1883 100644 --- a/micropython/examples/servo2040/single_servo.py +++ b/micropython/examples/servo2040/single_servo.py @@ -1,10 +1,9 @@ import time import math -from servo import Servo +from servo import Servo, servo2040 # Create a servo on pin 0 -PIN = 0 -s = Servo(PIN) +s = Servo(servo2040.SERVO_1) # Enable the servo (this puts it at the middle) s.enable() diff --git a/micropython/modules/servo/servo.c b/micropython/modules/servo/servo.c index 124ebbca..8dc3ac03 100644 --- a/micropython/modules/servo/servo.c +++ b/micropython/modules/servo/servo.c @@ -166,25 +166,52 @@ typedef struct _mp_obj_float_t { mp_obj_base_t base; mp_float_t value; } mp_obj_float_t; -mp_obj_float_t servo2040_shunt_resistor = {{&mp_type_float}, 0.015f}; +mp_obj_float_t servo2040_shunt_resistor = {{&mp_type_float}, 0.003f}; /***** Globals Table *****/ STATIC const mp_map_elem_t servo2040_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_servo2040) }, - { MP_ROM_QSTR(MP_QSTR_LED_R), MP_ROM_INT(16) }, - { MP_ROM_QSTR(MP_QSTR_LED_G), MP_ROM_INT(17) }, - { MP_ROM_QSTR(MP_QSTR_LED_B), MP_ROM_INT(18) }, - { MP_ROM_QSTR(MP_QSTR_BUTTON_A), MP_ROM_INT(12) }, - { MP_ROM_QSTR(MP_QSTR_BUTTON_B), MP_ROM_INT(13) }, - { MP_ROM_QSTR(MP_QSTR_USER_SW), MP_ROM_INT(23) }, - { MP_ROM_QSTR(MP_QSTR_CLK), MP_ROM_INT(14) }, - { MP_ROM_QSTR(MP_QSTR_DAT), MP_ROM_INT(15) }, - { MP_ROM_QSTR(MP_QSTR_CURRENT_SENSE), MP_ROM_INT(29) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_1), MP_ROM_INT(0) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_2), MP_ROM_INT(1) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_3), MP_ROM_INT(2) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_4), MP_ROM_INT(3) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_5), MP_ROM_INT(4) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_6), MP_ROM_INT(5) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_7), MP_ROM_INT(6) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_8), MP_ROM_INT(7) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_9), MP_ROM_INT(8) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_10), MP_ROM_INT(9) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_11), MP_ROM_INT(10) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_12), MP_ROM_INT(11) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_13), MP_ROM_INT(12) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_14), MP_ROM_INT(13) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_15), MP_ROM_INT(14) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_16), MP_ROM_INT(15) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_17), MP_ROM_INT(16) }, + { MP_ROM_QSTR(MP_QSTR_SERVO_18), MP_ROM_INT(17) }, + { MP_ROM_QSTR(MP_QSTR_LED_DAT), MP_ROM_INT(18) }, + { MP_ROM_QSTR(MP_QSTR_NUM_LEDS), MP_ROM_INT(6) }, + { MP_ROM_QSTR(MP_QSTR_INT), MP_ROM_INT(19) }, { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_INT(20) }, { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_INT(21) }, - + { MP_ROM_QSTR(MP_QSTR_USER_SW), MP_ROM_INT(23) }, + { MP_ROM_QSTR(MP_QSTR_ADC_ADDR_0), MP_ROM_INT(22) }, + { MP_ROM_QSTR(MP_QSTR_ADC_ADDR_1), MP_ROM_INT(24) }, + { MP_ROM_QSTR(MP_QSTR_ADC_ADDR_2), MP_ROM_INT(25) }, + { MP_ROM_QSTR(MP_QSTR_ADC0), MP_ROM_INT(26) }, + { MP_ROM_QSTR(MP_QSTR_ADC1), MP_ROM_INT(27) }, + { MP_ROM_QSTR(MP_QSTR_ADC2), MP_ROM_INT(28) }, + { MP_ROM_QSTR(MP_QSTR_SHARED_ADC), MP_ROM_INT(29) }, + { MP_ROM_QSTR(MP_QSTR_SENSOR_1_ADDR), MP_ROM_INT(0b000) }, + { MP_ROM_QSTR(MP_QSTR_SENSOR_2_ADDR), MP_ROM_INT(0b001) }, + { MP_ROM_QSTR(MP_QSTR_SENSOR_3_ADDR), MP_ROM_INT(0b010) }, + { MP_ROM_QSTR(MP_QSTR_SENSOR_4_ADDR), MP_ROM_INT(0b011) }, + { MP_ROM_QSTR(MP_QSTR_SENSOR_5_ADDR), MP_ROM_INT(0b100) }, + { MP_ROM_QSTR(MP_QSTR_SENSOR_6_ADDR), MP_ROM_INT(0b101) }, + { MP_ROM_QSTR(MP_QSTR_VOLTAGE_SENSE_ADDR), MP_ROM_INT(0b110) }, + { MP_ROM_QSTR(MP_QSTR_CURRENT_SENSE_ADDR), MP_ROM_INT(0b111) }, { MP_ROM_QSTR(MP_QSTR_SHUNT_RESISTOR), MP_ROM_PTR(&servo2040_shunt_resistor) }, - { MP_ROM_QSTR(MP_QSTR_ADC_GAIN), MP_ROM_INT(50) }, + { MP_ROM_QSTR(MP_QSTR_ADC_GAIN), MP_ROM_INT(69) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_servo2040_globals, servo2040_globals_table);