Skip to main content

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:

  • degree ranges from 0~70
  • 0 is closer to fully open
  • 70 is 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 GPIO22 is occupied by camera-related functions
  • LED index i starts from 1
  • color_from_rgb() returns an integer in 0xRRGGBB format
  • 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 value
  • dlight_lux() returns converted lux

Voice recognition

cm.asr_get_command()
cm.asr_play_command(cmd_id)

Notes:

  • asr_get_command() returns the latest command ID
  • it usually returns 0 when 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_module enters camera mode and gripper control switches from normal PWM to I2C
  • GPIO22 is shared by the servo, NeoPixel, and I2C SCL. Do not debug NeoPixel and I2C peripherals together at first; validate them one by one