Skip to content

Sonar/IR raytracing WIP#29

Draft
dwoodv2 wants to merge 9 commits into
legorovers:masterfrom
dwoodv2:raytrace
Draft

Sonar/IR raytracing WIP#29
dwoodv2 wants to merge 9 commits into
legorovers:masterfrom
dwoodv2:raytrace

Conversation

@dwoodv2

@dwoodv2 dwoodv2 commented Oct 29, 2024

Copy link
Copy Markdown
Contributor

This is my ray tracing implementation for the sonar sensor. I have constructed a wave tracing model which simulated the movement of waves and the absorptive properties of waves: see sonar.py, ray.py and material_constants.py

To view this, just run examples/SonarAvoider.py and then run pysim.py as normal.

This solution mostly works but there is still stuff TODO:

  1. add gaussian noise / variance e.g. vary the positions of the ray's origin using gaussian noise (use np.random.normal()?)
  2. use OpenGL/Pyglet to draw the rays
  3. Verify if the constants defined in material_constants.py are accurate (could do this experimentally in real life?)
  4. For some reason, out of bound rays are never detected by the sensor (probably a bug?)
  5. Extend ray tracing to the IR sensor (this will use the same ray tracing algorithm as the sonar but intensity doesn't matter as it is a ray of light

@louiseadennis louiseadennis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had a go at running this and, as you mentioned in the issue discussion, it's pretty resource intensive. There is no need to be constantly updating the sonar - you can link it to calls to getDistance() used by the user/programmer. This is how the actual robots work as well so it makes sense in several ways.

Comment thread src/sensors/sonar.py Outdated
ymap = int(ray.y / self.sensor_map.resolution)
# Check if ray is out of bounds, it should bounce.
if xmap < 0 or xmap >= self.sensor_map.width or ymap < 0 or ymap >= self.sensor_map.height:
print(f"Ray {ray_num} (out of bounds) bounced at: ({x1},{y1})!")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put some kind of if statement around this so it only prints when in debug or developer mode (or similar). It will be distracting for a child if they happen to be looking at the terminal to see this printed out.

Comment thread src/sensors/sonar.py
self.beams = beams
self.current_range = -1.0

def update_sonar(self, x, y, theta):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we link this to the getDistance() method in the API for the two robots? That way it is only called when getDistance() is explicitly invoked by the user/programmer. This will make everything much more efficient and prevent other functions being held up because update_sonar is constantly executing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leaving a note to remind myself and inform others - that this doesn't work in the simulator. The getDistance method is on the "python program" with no direct link to the simulated robot. The robot's overall state on the python program side is updated by reading from a socket link that the simulated robot publishes to regularly. We would need to rework the entire communication pattern to have distance only calculated on request.

@dwoodv2

dwoodv2 commented Nov 23, 2024

Copy link
Copy Markdown
Contributor Author

I did some performance analysis and it takes around 200-300ms to update the sensors. If I want to further improve the accuracy of the simulation, I want to get this delay down. I looked at the ray tracing library (https://pypi.org/project/raytracing/) which could allow me to do multithreading/utilize GPU acceleration but this is hardware specific. This library also has some nice integration with Matplotlib which will be really useful for debugging and showing the kids how the sonar blind spots occur.

I also feel like the code should be refactored first (I could create a UML diagram for the new structure), but I'll make a draft using this new library in the near future when I finally have time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants