BrickPi EV3 Touch Sensor and Motors

Hi, I’m only relatively new to the world of BrickPi and have recently updated the firmware on my device as so it can accept the EV3 sensors which I have with my Lego Mindstorms kit.

Thought I might share some code that I’ve put together. It uses the motors plugged into MA and MB and the EV3 touch sensor plugged into S4.

Pressing the button will spin the motors forward, pressing the button again will spin the motors in reverse and so on and so forth.

To run it, place the attached code under Desktop/BrickPi_Python/SensorExamples and call it say, Switch-Motor.py

call the code by issuing the following line:
sudo python Switch-Motor.py

It’s only a simple example, but I’m going to look into the Infrared and the Colour Sensor next and see what I can come up with.

Anyway, in the meantime, I hope this example helps others out with the EV3 sensors.

from BrickPi import * #import BrickPi.py file to use BrickPi operations

BrickPiSetup() # setup the serial port for communication

BrickPi.MotorEnable[PORT_A] = 1 #Enable the Motor A
BrickPi.MotorEnable[PORT_B] = 1 #Enable the Motor B

BrickPi.SensorType[PORT_4] = TYPE_SENSOR_EV3_TOUCH_DEBOUNCE #Set the type of sensor at PORT_4

BrickPiSetupSensors() #Send the properties of sensors to BrickPi

power = 200
buttonstatus=0
buttonpress=0

while True:
BrickPi.MotorSpeed[PORT_A] = power #Set the speed of MotorA (-255 to 255)
BrickPi.MotorSpeed[PORT_B] = power #Set the speed of MotorB (-255 to 255)
BrickPiUpdateValues()
r1 = BrickPi.Sensor[PORT_4]
BrickPiUpdateValues()
r2 = BrickPi.Sensor[PORT_4]
BrickPiUpdateValues()
r3 = BrickPi.Sensor[PORT_4]

	if (r1==1 and r2==1 and r3==1): #The button has been pressed
	if buttonstatus==0: # button has been pressed since last sampling
		power=-power	#Get ready to reverse the motor
		buttonstatus=1	#Remember the button has been pressed.
		buttonpress=1
	
	else:
	if buttonpress==1:	#button has been released in this sampling and had currently been recorded as pressed
		buttonstatus=0	#Remember the button has now been released.
	
	time.sleep(.1)

Hey TheBrad,

Thanks so much for sharing this! This is really fantastic.

So you were able to update the firmware on the BrickPi; I’m afraid to ask, but how was it? :slight_smile:

Would you mind putting this into a python example and doing a pull request on it? It might be really helpful to others using the library and would get automatically published with the new image and in our libraries. We would love that, and we would give you full credit!

Thanks again for sharing this!

John

Hello TheBrad,

did you encounter any trouble with EV3 Touch Sensor?
Why did you upgrade? Was the check firmware script reporting that current firmware isn’t supporting EV3 Sensors?

I’m having trouble with EV3 Trouch Sensor and don’t know the reason - either BrickPi or sensor itself.

I tested both C and Python examples and noticed big diffrences in behaviour. C is working better with Ultrasound and Gyro, but both fail to work with Touch.

What RPi version are you using?

Hi Kerhold,

Don’t know if any info that I can provide will assist you given the time that has passed since your response written back in March (apologies about that, the BrickPi has been literaly sidelined until now due to time constraints).

Anyway, I’m using a RPi B model, the firmware upgrade was performed using the Arduino Uno (in Australia I picked up it and the necessay other parts from Jaycar) and by following the instructions provided on the Dexter Industries site. The upgrade is a necessary step in order to run the EV3 sensors, the Dexter site also provides examples for the EV3 sensors as well (it is how I put together the code that I generated in my original post). FYI Running the check, all came back successful.

Python vs C. Compiled C code will always run faster as opposed to Python which executes code on the run, I like Python as I’m lazy and a bit of a hack coder.

Anyway, I haven’t any issue with the Touch Sensor with the upgraded firmware on my setup (or any of the other sensors for that matter). FYI As a side note, I’ve also written a bit of code (also in Python) to correlate the light level detected by the colour sensor and increase/decrease the motor speed accordingly (haven’t created a project to go with it as of yet…but baby steps).

Hope this of some help, and once again apologies about the delay in my response,

Regards,
Brad