Reading the Color Sensor and the Virtual World
Session 3 of the AFNOG tutorial
on IoT sensors
Uli Raich
COPYRIGHT © 2024 by the contributing authors
Slide 1 of 17
How to create the Virtual World
We have seen the image of
- the ultra-sonic sensor
- the color sensor
- the colored paper
Having a closer look we see that it is composed of simple 3d objects:
COPYRIGHT © 2024 by the contributing authors
Slide 2 of 17
Some of the examples in the slides are videos captured from the screen.
It is more difficult to insert these into the TWiki pages than into the slides.
I uploaded mpeg video clips and added links such by clicking them
you can download and/or visualize the clips.
COPYRIGHT © 2024 by the contributing authors
Slide 3 of 17
COPYRIGHT © 2024 by the contributing authors
Slide 4 of 17
The documentation of VPython explains how to create and change the objects
You can find it at
vpython.org
Now we will go through a few simple examples which will enable us to assemble the scene.
COPYRIGHT © 2024 by the contributing authors
Slide 5 of 17
First we must import the methods allowing to create the objects and then create the cylinder.
COPYRIGHT © 2024 by the contributing authors
Slide 6 of 17
When running the code you will see a warning:
It turns out that I did not have this problem on my older version of
Raspbian which runs a slightly older version of Python.
It must have appeared only with a very recent Python-2.7 version.
It is only a warning and does not prevent the program from running.
If it annoys you, you have 2 options:
- Correct the code of VPython or
- simply suppress the warning
The warning can be suppressed by the following code:
COPYRIGHT © 2024 by the contributing authors
Slide 7 of 17
Now we want to add a sphere at position 0,0,0 and move the cylinder to the left of the x-axis.
COPYRIGHT © 2024 by the contributing authors
Slide 8 of 17
Next we want to make the cylinder longer but slimmer,
change the color of the sphere and make it real small.
COPYRIGHT © 2024 by the contributing authors
Slide 9 of 17
Finally let’s add a virtual paper sheet in form of a very thin (in x-direction) box
simulating the paper sheet:
COPYRIGHT © 2024 by the contributing authors
Slide 10 of 17
Once the static part is done we have to figure out how to move the paper
(depending of the distance of the real paper to the distance detector)
and how to change its color (depending on the color found by the color sensor)
Let's first do this without reading the sensors by just displaying the paper at a certain
position before moving it on and by changing its color and displaying it for a time before
changing it.
and here is the corresponding video clip again:
https://iotworkshop.africa/pub/AFNOG/VPythonAndTheVirtualWorld/movePaper.mpeg
COPYRIGHT © 2024 by the contributing authors
Slide 11 of 17
COPYRIGHT © 2024 by the contributing authors
Slide 12 of 17
COPYRIGHT © 2024 by the contributing authors
Slide 13 of 17
A model of the distance sensor
This is the model of the ultrasonic distance sensor.
First the blue base plate is creates on which we have 2 cylinders
representing the loudspeaker and the microphone.
On top of each white cylinder I put a very thin black cylinder.
In addition we see the quartz implemented as a box
Understanding this code will allow you to easily create the model of the color sensor.
COPYRIGHT © 2024 by the contributing authors
Slide 14 of 17
Integrating the measurements
To be added:
- Create a pi object connecting our program to the pigpiod daemon
- Create a colorSensor instance
- Create a usSensor instance (us stands for ultra-sonic)
- Calibrate the colorSensor with black and white
In an endless loop:
- Read the distance sensor
- If the paper is close to the distance sensor, read the color
- Adapt the distance and the color of the model paper
COPYRIGHT © 2024 by the contributing authors
Slide 15 of 17
Color reading only close by
Why do we read the color only when the paper is close to the color sensor?
The color found by the sensor depends on the light intensity
reflected by the paper and this changes with distance
If we measure the color far away from the sensor little light will fall
onto the sensor and the color will come out dark.
Therefore put the paper close to the color sensor at first and keep the same
color when the paper is moving away from the sensor.
COPYRIGHT © 2024 by the contributing authors
Slide 16 of 17
Enough lectures, enough talking!
It is time to experiment. Hopefully you now have the information needed to play with
- the Python language
- sensors and actuators
- VPython to create realistic models
Of course 2 days are too short to go into detail of IoT sensors and actuators.
The Raspberry Pi offers you many more possibilities:
- different hardware interfaces
- I2C bus
- SPI bus
- camera interface ....
- and of course you can write your IoT code in a different language
COPYRIGHT © 2024 by the contributing authors
Slide 17 of 17
--
Uli Raich - 2018-04-25
Comments
This topic: AFNOG
> WebHome >
Slides > VPythonAndTheVirtualWorld
Topic revision: r1 - 2018-04-25 - UliRaich
Copyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback