Tags:
create new tag
view all tags
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://afnog.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’s 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://afnog.iotworkshop.africa/pub/AFNOG/VPythonAndTheVirtualWorld/movePaper.mpeg

Slide 12: Change the Paper Color

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

https://afnog.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://afnog.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

-- Uli Raich - 2018-04-25

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng VPythonDoc.png r1 manage 97.5 K 2018-04-25 - 17:36 UliRaich  
Quicktime moviempeg colors.mpeg r1 manage 1244.0 K 2018-04-25 - 17:37 UliRaich  
PNGpng cylinder-1.png r1 manage 21.6 K 2018-04-25 - 17:35 UliRaich  
PNGpng cylinder-1Code.png r1 manage 10.1 K 2018-04-25 - 18:04 UliRaich  
PNGpng cylinderAndSphere.png r1 manage 10.2 K 2018-04-25 - 17:35 UliRaich  
PNGpng cylinderAndSphereCode.png r1 manage 11.8 K 2018-04-25 - 18:14 UliRaich  
PNGpng distanceSensorModelCode.png r1 manage 40.4 K 2018-04-25 - 18:34 UliRaich  
PNGpng futureWarning.png r1 manage 22.4 K 2018-04-25 - 18:04 UliRaich  
Quicktime moviempeg label.mpeg r1 manage 864.0 K 2018-04-25 - 17:38 UliRaich  
PNGpng moveAndResize.png r1 manage 21.5 K 2018-04-25 - 17:35 UliRaich  
Quicktime moviempeg movePaper.mpeg r1 manage 612.0 K 2018-04-25 - 17:39 UliRaich  
PNGpng paperColorCode.png r1 manage 58.5 K 2018-04-25 - 18:41 UliRaich  
PNGpng paperMove.png r1 manage 2.7 K 2018-04-25 - 18:29 UliRaich  
PNGpng paperMoveCode.png r1 manage 48.2 K 2018-04-25 - 18:29 UliRaich  
PNGpng scene-1.png r1 manage 16.4 K 2018-04-25 - 17:35 UliRaich  
PNGpng sizeAndColor.png r1 manage 6.6 K 2018-04-25 - 18:18 UliRaich  
PNGpng sizeAndColorCode.png r1 manage 19.9 K 2018-04-25 - 18:18 UliRaich  
PNGpng suppressWarning.png r1 manage 9.7 K 2018-04-25 - 18:04 UliRaich  
PNGpng vPythonTextCode.png r1 manage 71.2 K 2018-04-25 - 18:46 UliRaich  
PNGpng virtualPaper.png r1 manage 7.1 K 2018-04-25 - 18:21 UliRaich  
PNGpng virtualPaperCode.png r1 manage 29.7 K 2018-04-25 - 18:21 UliRaich  
Quicktime moviempeg virtualWorld.mpeg r1 manage 732.0 K 2018-04-25 - 17:39 UliRaich  
PNGpng vpythonText.png r1 manage 5.1 K 2018-04-25 - 18:50 UliRaich  
PNGpng vpythonText.png.png r1 manage 5.1 K 2018-04-25 - 18:46 UliRaich  
Topic revision: r1 - 2018-04-25 - UliRaich
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 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