Start presentation

Slide 1: Reading the Color Sensor and the Virtual World

Session 3 of the AFNOG tutorial
on IoT sensors

Uli Raich

Slide 2: 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:

  • cylinders
  • boxes
  • a sphere
scene-1.png

Slide 3: Videos

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.

Slide 4: VPython

VPython makes it easy to create and display these 3d objects.

VPython creates the scene in a separate windows and

  • Lets you move the viewpoint (3rd mouse button)
  • Lets you scale the image (2nd mouse button)
moveAndResize.png

and here is the video clip:

https://iotworkshop.africa/pub/AFNOG/VPythonAndTheVirtualWorld/virtualWorld.mpeg

Slide 5: VPython Doc

The documentation of VPython explains how to create and change the objects

VPythonDoc.png

You can find it at vpython.org

Now we will go through a few simple examples which will enable us to assemble the scene.

Slide 6: Create a cylinder

First we must import the methods allowing to create the objects and then create the cylinder.
cylinder-1Code.png cylinder-1.png

Slide 7: The FutureWarning

When running the code you will see a warning:

futureWarning.png

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:

suppressWarning.png

Slide 8: Add a sphere

Now we want to add a sphere at position 0,0,0 and move the cylinder to the left of the x-axis.
cylinderAndSphereCode.png cylinderAndSphere.png

Slide 9: Change Size and Color

Next we want to make the cylinder longer but slimmer,

change the color of the sphere and make it real small.
sizeAndColorCode.png sizeAndColor.png

Slide 10: The Virtual Paper Sheet

Finally let\x92s add a virtual paper sheet in form of a very thin (in x-direction) box

simulating the paper sheet:
virtualPaperCode.png virtualPaper.png

Slide 11: Move the paper

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.
paperMoveCode.png paperMove.png
and here is the corresponding video clip again:

https://iotworkshop.africa/pub/AFNOG/VPythonAndTheVirtualWorld/movePaper.mpeg

Slide 12: Change the Paper Color

paperColorCode.png virtualPaper.png
again, here is the video clip:

https://iotworkshop.africa/pub/AFNOG/VPythonAndTheVirtualWorld/colors.mpeg

Slide 13: Text in VPython

In our case it might be interesting to print the current distance to the paper in text form.
vPythonTextCode.png vpythonText.png
the video clip shows the result:

https://iotworkshop.africa/pub/AFNOG/VPythonAndTheVirtualWorldlabel.mpeg

Slide 14: 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.

distanceSensorModelCode.png

Slide 15: 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

Slide 16: 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.

Slide 17: Time to experiment

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

-- %USERSIG{UliRaich - 2018-04-25}%

Comments

 

I Attachment Action Size Date Who Comment
VPythonDoc.pngpng VPythonDoc.png manage 97 K 25 Apr 2018 - 17:36 UnknownUser  
colors.mpegmpeg colors.mpeg manage 1 MB 25 Apr 2018 - 17:37 UnknownUser  
cylinder-1.pngpng cylinder-1.png manage 21 K 25 Apr 2018 - 17:35 UnknownUser  
cylinder-1Code.pngpng cylinder-1Code.png manage 10 K 25 Apr 2018 - 18:04 UnknownUser  
cylinderAndSphere.pngpng cylinderAndSphere.png manage 10 K 25 Apr 2018 - 17:35 UnknownUser  
cylinderAndSphereCode.pngpng cylinderAndSphereCode.png manage 11 K 25 Apr 2018 - 18:14 UnknownUser  
distanceSensorModelCode.pngpng distanceSensorModelCode.png manage 40 K 25 Apr 2018 - 18:34 UnknownUser  
futureWarning.pngpng futureWarning.png manage 22 K 25 Apr 2018 - 18:04 UnknownUser  
label.mpegmpeg label.mpeg manage 864 K 25 Apr 2018 - 17:38 UnknownUser  
moveAndResize.pngpng moveAndResize.png manage 21 K 25 Apr 2018 - 17:35 UnknownUser  
movePaper.mpegmpeg movePaper.mpeg manage 612 K 25 Apr 2018 - 17:39 UnknownUser  
paperColorCode.pngpng paperColorCode.png manage 58 K 25 Apr 2018 - 18:41 UnknownUser  
paperMove.pngpng paperMove.png manage 2 K 25 Apr 2018 - 18:29 UnknownUser  
paperMoveCode.pngpng paperMoveCode.png manage 48 K 25 Apr 2018 - 18:29 UnknownUser  
scene-1.pngpng scene-1.png manage 16 K 25 Apr 2018 - 17:35 UnknownUser  
sizeAndColor.pngpng sizeAndColor.png manage 6 K 25 Apr 2018 - 18:18 UnknownUser  
sizeAndColorCode.pngpng sizeAndColorCode.png manage 19 K 25 Apr 2018 - 18:18 UnknownUser  
suppressWarning.pngpng suppressWarning.png manage 9 K 25 Apr 2018 - 18:04 UnknownUser  
vPythonTextCode.pngpng vPythonTextCode.png manage 71 K 25 Apr 2018 - 18:46 UnknownUser  
virtualPaper.pngpng virtualPaper.png manage 7 K 25 Apr 2018 - 18:21 UnknownUser  
virtualPaperCode.pngpng virtualPaperCode.png manage 29 K 25 Apr 2018 - 18:21 UnknownUser  
virtualWorld.mpegmpeg virtualWorld.mpeg manage 732 K 25 Apr 2018 - 17:39 UnknownUser  
vpythonText.pngpng vpythonText.png manage 5 K 25 Apr 2018 - 18:50 UnknownUser  
vpythonText.png.pngpng vpythonText.png.png manage 5 K 25 Apr 2018 - 18:46 UnknownUser  
This topic: AFNOG > WebHome > Slides > VPythonAndTheVirtualWorld
Topic revision: 25 Apr 2018, UliRaich