Some months back Grovepi+ Drivers changed i2c so that when using multiple sensors, mutex is now required.
I understand that a mutex script has been written to allow multiple i2c devices to run without IOError: [Errno 121]. I have searched for many times for information as how to use Grove Mutex, without any luck.
The project I am working on requires the main programme to use two modules one of them reads ic2 devices continuously in 5 second infinite loop, and the other reads ic2 devices every 5 seconds, data use by the main script.
I have prevent IOError: [Err no 121] by using one script to read the devices, by using threading and locks however, the locks delays the speed at which the continuous loop reads some of the sensors, causing a loss of data.
Is there any way to prevent this and would your mutex help?
If it will do you have an idiots guide explaining how to use Grove mutex with multiple modules using i2c devices.
Note the scripts are written using Python3
Hoping for some help
Until DI answers: I don’t see a mutex test directory for the Grovepi+, but perhaps the GoPiGo3 mutex text examples can help you create two processes with the mutex to test how fast you can run each process: https://github.com/DexterInd/GoPiGo3/tree/master/Software/Python/tests
The di_i2c (Dexter Industries I2C) drivers protect the I2C bus for multi-threading and multi-processing, to prevent protocol corruption. However, the mutex protection in di_i2c only protects the bus, not the individual devices on the bus.
You can use di_mutex to protect individual devices, or if you’re using our sensors supported by DI_Sensors, you can use the “easy_” drivers that already have mutex protection built in.
To manually add mutex protection for a device (or any other resource for that matter), acquire the device mutex before every transaction, and release the device mutex immediately after the transaction. The acquire method will cause the thread to wait until the mutex has been released by whatever other thread was using it. The release method will release control of the mutex to allow another thread to use the resource. A mutex protected resource would look something like this:
while true: # loop
acquire # acquire exclusive access to the resource
read sensor # use the resource
release # allow another thread to use the resource
sleep 100ms # slow down so we don't hog resource time
Using di_mutex in Python, it could look something like this:
import time # for the sleep method
import di_mutex # import the mutex package
mutex_resource_1 = di_mutex.DI_Mutex(name = "resource_1") # create a mutex, and specify a mutex name, such as "resource_1"
mutex_resource_1.acquire() # acquire the mutex to ensure it's safe to use the resource
# access the resource protected by mutex_resource_1
mutex_resource_1.release() # release the mutex so another thread can use the resource
time.sleep(0.1) # share access to the resource
Thanks for your replies,
Am I writing in thinking that I need mutex with grove pi to use multiple i2c sensors and others.
While playing with my sensor programmes I keep getting NO ACK, I,m not what sure means and if mutex would solve that.
Thanks for sending the examples something I’m a little unclear about, should mutex be added to each driver, or to my script that reads them?
Since many of my Grove Sensors are not part of DI_Sensors, will this mutex work with them?