CB3 botcore – CodeBot CB3 hardware interface

Core low-level module for CodeBot-CB3.

The botcore module gives you direct access to all the hardware on the CodeBot CB3.
class CB3.botcore.Buttons[source]

Access to pushbutton switches BTN-0 and BTN-1.

is_pressed(num=None)[source]

Return True if specified button is pressed, or tuple of both buttons if num=None.

Parameters

num (int) – 0 for BTN-0, 1 for BTN-1

was_pressed(num=None)[source]

Return True if specified button was pressed since last call of this function. Default with num=None, returns tuple of both buttons status since last call.

Parameters

num (int) – 0 for BTN-0, 1 for BTN-1

class CB3.botcore.CB_LEDs[source]

Manage all the LEDs on CodeBot.

enc_emit(b)[source]

Set both Encoder emitter LEDs (2-bit value).

Parameters

b (int) – Binary value, 0=off, 1=left, 2=right, 3=both

ls(b)[source]

Set all User Line Sensor marker LEDs to the given binary integer value, or list/tuple of bools.

Parameters
  • bint value used to set LEDs, from 0 to 31 (2 5-1), or

  • blist of 5 bools. (see botcore.CB_LEDs.user for example)

ls_emit(ls_num, pwr)[source]

Set Line Sensor infrared emitter LED to specified power level.

Parameters
  • ls_num (int) – Number of Line Sensor 0-4

  • pwr (int) – Power level 0-2

ls_num(num, val)[source]

Set single Line Sensor marker LED.

Parameters
  • num (int) – Number of LED bit, 0-4.

  • val (bool) – Value of LED (True or False)

prox(b)[source]

Set both Proximity marker LEDs.

Parameters
  • bint binary value, 0=off, 1=left, 2=right, 3=both ; or,

  • blist of 2 bools. (see botcore.CB_LEDs.user for example)

prox_num(num, val)[source]

Set single Proximity marker LED.

Parameters
  • num (int) – Number of LED bit, 0-1.

  • val (bool) – Value of LED (True or False)

pwr(is_on)[source]

Set Power LED.

Parameters

is_on (bool) – Turn ON if True

usb(is_on)[source]

Set USB LED.

Parameters

is_on (bool) – Turn ON if True

user(b)[source]

Set all User Byte LEDs to the given binary integer value or list/tuple of bools.

Parameters
  • bint value used to set LEDs, from 0 to 255 (2 8-1) ; or,

  • blist of 8 bools.

Example:

leds.user(0)    # turn all User LEDs off
leds.user(255)  # turn all User LEDs on
leds.user(0b10101010)  # Alternating LEDs
leds.user( [True, False, False, True, True, True, True, True] )
user_num(num, val)[source]

Set single User Byte LED.

Parameters
  • num (int) – Number of LED bit, 0-7.

  • val (bool) – Value of LED (True or False)

class CB3.botcore.Encoders(leds)[source]

Manage the wheel encoders. This class demonstrates simple low-level sensing of encoders via ADC channels. Higher performance implementations are available in separate modules, such as timed_encoders.

is_slot(num)[source]

Check for slot in given encoder disc

read(num)[source]

Read given analog encoder value

class CB3.botcore.Expansion[source]

Access to the Expansion Port

make_pwm(pin, duty_cycle=0, frequency=50)[source]

Connect a PWM (pulse width modulation) peripheral to the specified pin. This can be used to control servos, or as a general-purpose oscillating output.

Parameters
  • pin – The IO pin to use, for example pin.EXP_GPIO0

  • duty_cycle – duration pin is high during each period, with 65535 being 100%.

  • frequency – periodic oscillation frequency in Hz (cycles per second)

Returns

The PWMOut object bound to specified pin.

Return type

pwmio.PWMOut

release_pwm(pwm)[source]

Disconnect the internal PWM peripheral previously bound to a pin.

Parameters

pwm – A PWM peripheral created with make_pwm()

set_servo(pwm, position_pct)[source]

Set the position of a servo controlled with a PWM output. The PWM frequency must be 50Hz (default value of make_pwm()). Note: Many servos require position_pct value to exceed the ±100 range to reach their mechanical limits. Also an offset may be needed to center the servo.

Parameters
  • pwm – A PWM output created with make_pwm().

  • position_pct – Desired servo position with “nominal” range -100 to +100. A value of 0 will “center” the servo.

class CB3.botcore.LineSensors(leds)[source]

Manage the line sensors.

calibrate(threshold, is_reflective_line)[source]

Set parameters used to detect presence of a line.

Parameters
  • threshold (int) – Threshold of ADC reading, below which reflection is detected. 0-4095 (2 12-1)

  • is_reflective_line (bool) – Is the line reflective?

check(thresh=1000, is_reflective=False)[source]

Fast check of all line sensors against threshold(s). Controls emitter also.

Return a tuple of values for all line sensors. By default these are bool values indicating presence of a line, based on given parameters. See below for alternate behavior based on parameter types.

Parameters
  • thresh (int) – Threshold value to compare against sensor readings.

  • is_reflective (bool) – Set to True if the line being checked is reflective (white), False if not (black).

Returns

Collection of bool “line detected” values.

  • If thresh=0, returns raw ADC values (ints).

  • If is_reflective is an int value it is interpreted as thresh_upper, and the function returns -1,0,+1 for readings below/within/above thresholds.

Return type

tuple [5]

is_line(num)[source]

Check for presence of line beneath specified Line Sensor using current calibration parameters.

Parameters

num (int) – Number of Line Sensor to read (0-1)

read(num)[source]

Read the raw ADC value of selected Line Sensor.

Parameters

num (int) – Number of Line Sensor to read (0-1)

class CB3.botcore.Motors(i2c)[source]

Manage the Motors

enable(do_enable)[source]

Enable the motors.

Parameters

do_enable (bool) – Set True to allow motors to run.

run(num, pwr)[source]

Set specified motor to given power level.

Parameters
  • num (int) – Number of motor: 0 (LEFT) or 1 (RIGHT)

  • pwr (int) – Power -100% to +100% (neg=CW=reverse, pos=CCW=forward)

class CB3.botcore.Proximity(leds)[source]

Manage the proximity sensors

detect(power=1, sens=100)[source]

Return bool (L,R) of detection at given sensitivity level.

Parameters
  • power (int) – Emitter power (1-8).

  • sens (int) – Sensor sensitivity (0-100). Percent range from 0=least to 100=most sensitive.

Returns

tuple of (L, R) bool detect status.

range(samples=4, power=1, rng_low=0, rng_high=100)[source]

Scan proximity sensors and return 0-100% range for each. This function runs a successive approximation algorithm to find the minimum range at which an object is detected by each sensor independently.

Parameters
  • samples (int) – Number of samples to read. Larger values give more accuracy at the expense of a longer scan time.

  • power (int) – Emitter power (0-8). (0=OFF,… 8=MAX)

  • rng_low (int) – Lowest sensitivity range (0-100) to scan within.

  • rng_high (int) – Highest sensitivity range (0-100) to scan within

Returns

0-100 if object detected, or -1 if not.

  • If samples=1, returns bool (L, R) values.

  • Note: When samples=1 the rng_high value is not used. A single sample is taken at rng_low.

Return type

tuple of (L, R) int proximity values

class CB3.botcore.Speaker[source]

Control the Speaker

off()[source]

Stop output to speaker.

pitch(freq, duty=50)[source]

Play a tone on the speaker, with the specified frequency and duty-cycle.

This function uses the ESP32s2’s cosine generator with best-fit parameters. Expect some error in the output frequency.

Parameters
  • freq (int) – Frequency in Hertz

  • duty (int) – Not used by CodeBot3

class CB3.botcore.System[source]

General system-level functions

pwr_is_usb()[source]

Are we powered by USB or Battery? (based on Power switch)

Returns

0 (Battery) or 1 (USB)

Return type

int

pwr_volts()[source]

Measure power supply voltage (battery or USB)

Returns

Power supply voltage

Return type

float

temp_C()[source]

Measure the temperature in Celsius

Returns

degrees C

Return type

float

temp_F()[source]

Measure the temperature in Fahrenheit

Returns

degrees F

Return type

float