Tetrix Motor Response Delay and Inactivity

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


print “Started”

BrickPi.Address = [1,2]
BrickPi.Timeout = 100
BrickPi.SensorType [0] = 1

See TetrixControllers.h for usage of initTetrixControllerSettings

initTetrixControllerSettings(BrickPi,TETRIX_SENSOR_PORT, 1, 0x2)
initTetrixDCMotor(mLeftDrive, TETRIX_SENSOR_PORT, 0, 2) # Defines motor at Port 1, 1)
initTetrixDCMotor(mRightDrive, TETRIX_SENSOR_PORT, 0, 1) # Defines motor at Port 1, 1)

BrickPi.SensorType[PORT_2] = TYPE_SENSOR_TOUCH


while True:
print BrickPi.Sensor[PORT_2]
if BrickPi.Sensor[PORT_2] == 1:
x = time.time()
print "backing up"
while(time.time() - x < 5):
setTetrixDCMotorSpeed(BrickPi,mLeftDrive, -100)
setTetrixDCMotorSpeed(BrickPi,mRightDrive, 100)
x = time.time()
print "now I am turning"
while(time.time() - x < 5):
setTetrixDCMotorSpeed(BrickPi,mLeftDrive, -100)
setTetrixDCMotorSpeed(BrickPi,mRightDrive, -100)
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)
print “Ended”

The program has correct indents, but they didn’t transfer

Hey McArthurscott, I’m sorry about the security settings. Is there a way you can repost the code, renamed to a .txt file, rather than a .py file? I’m going to take a quick look at our security settings, I don’t know why it’s not letting .py programs post. It would be helpful to see the indentation.

Hi mcarthurscott,
I am not sure what is causing the problem but maybe the code in “while(time.time() – x < 5):” might be the culprit. Please upload the code in the TXT format as a normal text file so that we can have a better look at the problem.

As per the problem with the motors running after pressing Ctrl X, it might be because no signal being sent to them that the motors have to be stopped. Try catching the Keyboard exception to stop the motors before exiting. I am attaching a sample code for that.

I have attached the text file for further review. I’ll try the keyboard interrupt and see what happens. Thanks for helping on this!

We are trying to use the Tetrix chassis, Tetrix battery for motors with converter attached (stepped down to 9V 2A) to run the electronics, remote access using tightVNC, and Lego sensors and motors working. We have gotten it all working to some extent, but just have this programming issue. The students, and I, are new to Python and the Pi but have worked with RobotC quite a bit.

Based on your “tetrix_motor_interrupt.txt” file, I think we have figured out our problem. To this point, we didn’t realize that BrickPiUpdateValues() was the coding used to send the motor settings to the robot and thought that it was code for getting settings from the robot. Seeing that you had BrickPiUpdateValues() at the end of your code was the clue. Anyhow, both problems are now fixed. Thanks so much…I attached a picture of the setup.