cocube_module
import cocube_module as cm
What is this?
cocube_module handles CoCube expansion modules, mainly including:
- power control
- gripper
- NeoPixel light board
- ToF ranging
- light sensor
- voice recognition module
For many peripherals, it is recommended to start with:
cm.power_on()
Power
cm.power_on()
cm.power_off()
Module power is controlled by GPIO13.
Gripper
cm.gripper_open()
cm.gripper_close()
cm.gripper_degree(degree)
cm.gripper_stop()
Notes:
degreeranges from0~700is closer to fully open70is closer to fully closed- in normal mode it uses PWM on
GPIO22 - after the camera is initialized, gripper control switches to I2C mode
NeoPixel light board
It is recommended to attach it explicitly first:
cm.neopixel_attach(n=48)
If you do not attach it manually, most NeoPixel APIs auto-initialize with the default 48 LEDs.
Common APIs:
cm.neopixel_set_all(r, g, b)
cm.neopixel_set_all_color(color)
cm.neopixel_set(i, r, g, b)
cm.neopixel_set_color(i, color)
cm.neopixel_clear()
cm.neopixel_rotate(n=1)
cm.neopixel_brighten(i, delta)
cm.neopixel_brighten_all(delta)
cm.neopixel_shift_color(i, delta)
cm.neopixel_shift_all_colors(delta)
cm.color_from_rgb(r, g, b)
cm.random_color()
Notes:
- NeoPixel is unavailable in AI camera mode, because
GPIO22is occupied by camera-related functions - LED index
istarts from 1 color_from_rgb()returns an integer in0xRRGGBBformat- NeoPixel data also uses
GPIO22
ToF ranging
cm.tof_connected()
cm.tof_distance()
Return values:
- normal millimeter value on success
-1: invalid data-2: measurement timeout
Light sensor
cm.dlight_level()
cm.dlight_lux()
Notes:
dlight_level()returns the raw valuedlight_lux()returns convertedlux
Voice recognition
cm.asr_get_command()
cm.asr_play_command(cmd_id)
Notes:
asr_get_command()returns the latest command ID- it usually returns
0when there is no new command
Minimum example
import cocube_module as cm
cm.power_on()
cm.gripper_open()
cm.neopixel_attach(48)
cm.neopixel_set_all(0, 0, 255)
print(cm.tof_distance())
print(cm.dlight_level())
print(cm.asr_get_command())
Notes
- after camera
init(),cocube_moduleenters camera mode and gripper control switches from normal PWM to I2C GPIO22is shared by the servo, NeoPixel, and I2C SCL. Do not debug NeoPixel and I2C peripherals together at first; validate them one by one