BrickPiUpdateValues - what is this command good for?

Hi,

i have a question concerning the often used command

BrickPiUpdateValues()

in BrickPi scripts.
What does this line do? When do I have to use it?

I hope someone can help me with this. Thank You in advance!

Welcome! :slight_smile:
BrickPiUpdateValues updates the sensors readings.
So if you want to read a sensor you first need to run BrickPiUpdateValues then read the sensor. You’ll need to do that every time you want sensor readings.

@graykevinb Thank You really much for your quick and helpful answer! :slightly_smiling_face:

I have another question:
I program my BrickPi-Robot with Python and I would like to know how I can realize to process my sensor readings from my ultrasonic sensor and my touch sensor at the same time.
So far I used while-Loops in two Threads to read out the sensors but I think that is really ineffectual since it leads to high cpu load.
Is there a possibility to use Python Interrupts or Callbacks? Or are there other ways of requesting the sensor data and handling it?

Here is the part of my script:

...
def check_us():
    while True:
        BrickPiUpdateValues()
        if BrickPi.Sensor[PORT_1] < 200:
                print("Ultrasonic sensor trigger.")
                stop() #stop the roboter
        time.sleep(0.4)
        

def check_ts():
    while True:
        BrickPiUpdateValues()
        m1=BrickPi.Sensor[PORT_2]
        BrickPiUpdateValues()
        m2=BrickPi.Sensor[PORT_2]
        BrickPiUpdateValues()
        m3=BrickPi.Sensor[PORT_2]
        if (m1==1 and m2==1 and m3==1): #certainty of pressed touch sensor
                print("Touch sensor trigger.")
                stop() #stop the roboter
        time.sleep(0.2)


try:
    thread.start_new_thread( check_us, ())
    print("Started Thread 1")
    check_ts()

except:
    print("Error: unable to start thread")

...

I would really appreciate, if someone could help me with this, because I am really interested in programming the BrickPi-Robot! :grinning:

Your welcome.
I really don’t see why you need to run it in two threads. The sensors readings of course are not instant, but neither is light. Checking for the sensors in the same thread should be fine. Also just asking at the same time for more data doesn’t necessarily ensure you get your readings quicker. Sure you may be able to shave off a millisecond maybe, but not that much. At least in your situation.

For example for running two motors you don’t have a whole thread dedicated to each motor. You simply just set the motor speeds on two different lines:
BrickPi.MotorSpeed[PORT_C] = power
BrickPi.MotorSpeed[PORT_D] = power

It happens so quick it looks like they start at the same time.

However if you want the fastest response time theoretically possible while saving cpu you could do it. Basically you would need to create a function that all it does is read sensors and return it. Then run this function in a new thread. You could then take rapid fire readings, but I really don’t think it will make a big difference. Though if you decide to use threads check out
ThreadSafeBrickPi As the name implies it should work better with threads and the BrickPi.

Does that help? I admit that multithreading is not my expertise.

Ok, thanks for your helpful explanation.

As an alternative I dont rather want to use threads but a loop. So I should create a while loop? In this loop I could read sensors and control the motors. But then I have to use one if-clauses for each sensor right? Or is there a general other way to do this in Python?
So far I will test a few ideas today and post problems… :slightly_smiling_face:

Yes, you would create a while loop in thread if that’s what your asking. Its the easiest method.

As for your question about the if-statements: you could just literally merge your two functions. That would include two if-statements. If you want only one you could do it like this:

if (m1==1 and m2==1 and m3==1) or BrickPi.Sensor[PORT_1] < 200:
stop()

That combines it into one statement. Pretty short and concise!