PICAR-X Module changes

I’m making changes to modules.py in /home/user/robot-hat/build/lib/robot_hat also mirrored to /home/user/robot-hat/robot_hat. These files are local to the PI (not github).

I’ve modded this (for debugging)
distance = px.get_distance()
print(f"Distance: {distance}")
sc.set(“F”, distance)
in 13.app_control.py, this is to wipe out -ve numbers.

I’ve modded modules.py in both places to do this
def read(self, times=10):
sum_a = 0
prior_a = 0
for i in range(times):
a = self._read()
if a < 0:
a = prior_a
prior_a = a
sum_a = sum_a + a
average_a = sum_a / times
return average_a

Which I think should never return -ve numbers, but still does. I have either messed up the above code (new ro Python, not to a host of others).

What am I doing wrong.
Thanks.

I don’t have the github code in front of me that you are referring to but I assume as you are using “self” that you are using this inside a class? But I don’t see the class definition. If you are inside a class? then this stand alone should point you in the right direction.

import random

class Sensor:
def _read(self):
# Simulated sensor read
return random.randint(-10, 10)

def read(self, times=10):
sum_a = 0
prior_a = 0
for i in range(times):
a = self._read()
if a < 0:
a = prior_a
prior_a = a
sum_a += a
average_a = sum_a / times
return average_a

sensor = Sensor()
print(sensor.read(10))

If you’re not inside a class ? then it should look more like this stand alone version

import random

def _read():
# Simulate a sensor read (returns a value between -10 and 10)
return random.randint(-10, 10)

def read(times=10):
sum_a = 0
prior_a = 0
for i in range(times):
a = _read()
if a < 0:
a = prior_a
prior_a = a
sum_a += a
average_a = sum_a / times
return average_a

print(read(10))

It’s also possible that something is simply wrong with your indentation, but that isn’t visible in your post. Just compare yours with above examples

1 Like

I think I may have solved it.

Looks like other classes pulled in by import get dragged from /usr/local/lib/python3.11/dist-packages, which would explain why changes done here /home/user/robot-hat/robot_hat or other folders under /home/user (excepting the main program e.g. /home/user/picar-x/example/13.app_control.py) dont take.

Why they have so many folders with same file in is baffleing. They could have made it simpler, or documented how to add/mod code to existing modules.

Thanks for the help, its appreciated.

In a nutshell. The code you download from github is the “source code”. When you run install.py this code and any other setup requirements gets installed into the python packages system itself for subsequent import etc. They’re not simply mirrors

1 Like

Yes. It is recommended to modify the code in /home/user/robot-hat/robot_hat, and then run the library installation in the /home/user/robot-hat directory with: sudo pip3 install . --break.

Alternatively, for beginners, it is advisable to wrap a function outside to handle it instead of directly modifying the library’s code."

1 Like

Yes, that works. Thankyou.