BrickPython Example Programs

This section describes the BrickPython examples.

MotorController

class MotorController.MotorControllerApp[source]

Bases: BrickPython.TkApplication.TkApplication

Application to control a lego NXT motor on port A as a servo motor.

Type keystrokes into the application window to make the motor move: Numbers 0-9 make it move forward the corresponding number of quarter-turns. Lower case letters a-p make it go backward the corresponding number of quarter turns. Capital letter A stops the motor. Capital letters B-G make it go forward at a constant speed. B is a quarter turn per second, C a half turn, and so on. Letters xyz and XYZ adjust the settings for the PID Servo Motor algorithm: * X,x increase and decrease the ‘distance multiplier’ - the P setting. * Y,y increase and decrease the ‘speed multiplier’ - the D setting (I think). * Z,z increase and decrease the ‘Integrated distance multimplier’ - the I setting.

onKeyPress(event)[source]

Handle user keystroke

rotate(degrees)[source]

Rotate motor A through degrees

setSpeed(speed)[source]

Set the speed of motor A

DoorControl

class DoorControl.DoorControlApp[source]

Bases: BrickPython.TkApplication.TkApplication

Application to automatically open and close a door using a proximity sensor. It also closes and ‘locks’ it when C is pressed (so the sensor no longer opens it), starting again when S or O is pressed

The door is attached to a motor, which opens it by moving through 90 degrees, and closes it the same way.

The sensor is mounted above the door, so it detects approaching ‘peaple’.

onKeyPress(event)[source]

Handle key input from the user

openDoorWhenSensorDetected()[source]

Coroutine that waits until the sensor detects something nearby, then opens the door, keeps it open for 4 seconds, then closes it again and repeats.

It uses the flag doorLocked to determine whether the user has ‘locked’ the door.

SimpleApp

class SimpleApp.SimpleApp[source]

Bases: BrickPython.CommandLineApplication.CommandLineApplication

Simple command line example application

doActivity()[source]

Coroutine to rotate a motor forward and backward

Table Of Contents

Previous topic

BrickPython API

Next topic

BrickPython - Possible Next Steps

This Page