Problem reading UltraBoard values with threading

Forums:

Hi,

I just start my first python script for my robot (yetiborg with 4 ultrasonic sensors) and thunderborg motor control. My first code test offers a problem when reading the ultraborg values using threads. If I used time-sleep to better check the values readed all worked fine, if commented it, I got reading errors like nonreading , none type values, values with decimals like 567,3275839273747 etc. I added my scriupt and a kind of log, where you can see the errors. Any ideas, what the reason is? Many thx in advance.

One further question. How long measure the ultrasonic im meters? If there are more than I 4 or 5 meter I've got no value? How can I catch this in my script?

regardly

Peter

Attachments: 
piborg's picture

The reason you see values with decimals on occasion is because the readdistance thread updates the global values twice. This means the other thread can read the value after the first update, but before the second.

For example:

def readdistance():
	global act_dist1
	global act_dist2
	global act_dist3
	global act_dist4
	while True:
		act_dist1 = UB.GetDistance1()
		act_dist2 = UB.GetDistance2()
		act_dist3 = UB.GetDistance3()
		act_dist4 = UB.GetDistance4()
		# The control thread looks at the values at this point
		act_dist1 = int(act_dist1)
		act_dist2 = int(act_dist2)
		act_dist3 = int(act_dist3)
		act_dist4 = int(act_dist4)
		print("act_dist1 read ", act_dist1)
		print("act_dist2 read ", act_dist2)
		print("act_dist3 read ", act_dist3)
		print("act_dist4 read ", act_dist4)
		time.sleep(0.5)

Without the sleep this occurs more often, but it will happen occasionally even with the sleep. This can be fixed by only changing the global values once:

def readdistance():
	global act_dist1
	global act_dist2
	global act_dist3
	global act_dist4
	while True:
		read_dist1 = UB.GetDistance1()
		read_dist2 = UB.GetDistance2()
		read_dist3 = UB.GetDistance3()
		read_dist4 = UB.GetDistance4()
		act_dist1 = int(read_dist1)
		act_dist2 = int(read_dist2)
		act_dist3 = int(read_dist3)
		act_dist4 = int(read_dist4)
		print("act_dist1 read ", act_dist1)
		print("act_dist2 read ", act_dist2)
		print("act_dist3 read ", act_dist3)
		print("act_dist4 read ", act_dist4)
		time.sleep(0.5)

in this case the temporary read_dist values can only be seen by the readdistance thread, not the control thread.

In the readdistance thread you can also catch when the values failed to read:

def readdistance():
	global act_dist1
	global act_dist2
	global act_dist3
	global act_dist4
	while True:
		read_dist1 = UB.GetDistance1()
		read_dist2 = UB.GetDistance2()
		read_dist3 = UB.GetDistance3()
		read_dist4 = UB.GetDistance4()
		if read_dist1 is None:
			print("act_dist1 not updated")
		else:
			act_dist1 = int(read_dist1)
			print("act_dist1 read ", act_dist1)
		if read_dist2 is None:
			print("act_dist2 not updated")
		else:
			act_dist2 = int(read_dist2)
			print("act_dist2 read ", act_dist2)
		if read_dist3 is None:
			print("act_dist3 not updated")
		else:
			act_dist3 = int(read_dist3)
			print("act_dist3 read ", act_dist3)
		if read_dist4 is None:
			print("act_dist4 not updated")
		else:
			act_dist4 = int(read_dist4)
			print("act_dist4 read ", act_dist4)
		time.sleep(0.5)

This can happen if the UltraBorg failed to respond in time.

The HC-SR04 ultrasonic sensor has a maximum range of 4 meters. The distance returned should be 0 if the sensor did not get a valid reading.

Many thx for your tipps and hints!!!!!! Great.

regardly

Peter

Hi,

could it be possibel that the ultrasonic sensor doesn't recognize smaller things like chair legs and so on. I have no problems with wall, stores on the floor.
Many thx

regardly

Peter

piborg's picture

You are right, the ultrasonic sensors do struggle with small / narrow objects, especially when they are further away.

From experimenting they also work best when directly facing the flat surface, too much angle and they do not detect things at all :)

Hi

Can you recomment an alternative Which bettet Works?

Peter

piborg's picture

Unfortunately all ultrasonic sensors have this type of limitation, although some are better than others. Most sensors quote the range they will sense over and the angles they work for, but not how small an object they can detect :(

With small objects you may have more luck with an IR sensor or a LiDAR.

Almost all distance sensors work on signals reflected back, so they will all have some kind of angle limitation for sensing.

Subscribe to Comments for "Problem reading UltraBoard values with threading"