We have been working on getting our BrickPi to run with both Lego Motors and Tetrix motors at the same time. One of the problems we have encountered is that the Tetrix motors seem to have a delay in their response to the code. For example, using the sample program, the motors will continue to run for an additional 3-4 seconds after the program ends. With the program we have written, a simple one that uses a touch sensor to detect walls and have the robot back up and turn, the touch sensor will get pushed (since we have it printing out the sensor value on the screen) but the robot won’t respond to the touch right away.
In the program below, the robot will drive forward, but when the sensor is pressed, it will print “backing up”, wait 5 seconds with no motor activity, print “now I am turning” with no motor activity, and then continue to drive ahead.
When we press ctrl-c to break out of the loop, the motor will continue to drive ahead for 3-5 seconds after “Ended” is printed on the screen. Any help would be greatly appreciated.
Scott McArthur
Sitka High School Science and Robotics Teacher
Sitka, Alaska
from BrickPi import *
from TetrixController import *
import sys
import time
TETRIX_SENSOR_PORT=PORT_1
print “Started”
BrickPi.Address = [1,2]
BrickPi.Timeout = 100
BrickPi.SensorType [0] = 1
BrickPiSetup()
See TetrixControllers.h for usage of initTetrixControllerSettings
initTetrixControllerSettings(BrickPi,TETRIX_SENSOR_PORT, 1, 0x2)
mLeftDrive=TetrixDCMotor()
initTetrixDCMotor(mLeftDrive, TETRIX_SENSOR_PORT, 0, 2) # Defines motor at Port 1, 1)
mRightDrive=TetrixDCMotor()
initTetrixDCMotor(mRightDrive, TETRIX_SENSOR_PORT, 0, 1) # Defines motor at Port 1, 1)
BrickPi.SensorType[PORT_2] = TYPE_SENSOR_TOUCH
BrickPiSetupSensors()
while True:
BrickPiUpdateValues()
print BrickPi.Sensor[PORT_2]
if BrickPi.Sensor[PORT_2] == 1:
x = time.time()
print "backing up"
while(time.time() - x < 5):
BrickPiUpdateValues()
setTetrixDCMotorSpeed(BrickPi,mLeftDrive, -100)
setTetrixDCMotorSpeed(BrickPi,mRightDrive, 100)
x = time.time()
print "now I am turning"
while(time.time() - x < 5):
BrickPiUpdateValues()
setTetrixDCMotorSpeed(BrickPi,mLeftDrive, -100)
setTetrixDCMotorSpeed(BrickPi,mRightDrive, -100)
else:
setTetrixDCMotorSpeed(BrickPi,mLeftDrive, 100)
setTetrixDCMotorSpeed(BrickPi,mRightDrive, -100) #Right motor has to be negative to go forward
setTetrixDCMotorSpeed(BrickPi,mLeftDrive, 0)
setTetrixDCMotorSpeed(BrickPi,mRightDrive, 0)
time.sleep(1)
print “Ended”