I am dealing with a differential drive robot that interacts with several motors and actuators, all through the Jetson Xavier. I use Python3 for programming all actions of the robot that go through a microcontroller via CAN bus.
Now, there are multiple parts of the robot that we need to program; chiefly navigation and arm-movement. Right now, the robot does only either of them, i.e., it can either move or perform some arm-movement. All the actions are based on the same 1D LiDAR sensor.
To boost its speed, I thought of performing both these functions in parallel, as they are almost independent of the other. Python provides two wonderful options for this: multi-processing and threading. Both of them seem equally good options with the caveat that threads cannot be killed and prove negligible improvement when compared to multi-processing. So, I am staying with multi-processing for the time-being.
Now, the way I use the sensor or lidar values is, to get them via a thread and continuously dump them as global variables.
t = Thread(target=dump_lidar_data,args=())
t.start()
Various functions defined in the same file, use these values as:
def fun():
global lidar1
print(f'Look, I too can access lidar 1 values: {lidar1}')
The main process or the parent process detects when its show time and spawns two child processes: p_navigation
and p_arm
. Both these process start together and have their own instance of dump_lidar
working in their respective processors. Both terminate on the basis of the same condition. In the meantime, the parent process does nothing.
I notice that there is a considerable delay (upto 1s) between the data procured by the dump_lidar
in the parent process as compared to the ones in the child processes. It is also possible that the CAN bus is giving the data after a certain delay.