Difference: IntroductionToPython (3 vs. 4)

Revision 42018-04-25 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="Slides"

Start Presentation

Slide 1: Sensors for IoT devices … or A virtual world reflecting the real world

Line: 240 to 240
 idle.png

Changed:
<
<

The same thing as a script in idle

>
>

The same thing as a script in idle

  Here you see the 2 idle windows.
Line: 289 to 289
  conditions.png
Changed:
<
<
As you can see, there are no braces around the conditional block but it is indented
# indicates an comment
In addition to the if statement above we have elif and else
>
>
As you can see, there are no braces around the conditional block but it is indented

# indicates an comment

In addition to the if statement above we have elif and else

 

while loop

Line: 300 to 304
  while.png
Changed:
<
<
This program uses multiple assignments a,b are assigned in the same line
Again you can see that the block starts with “:” and is indented.
>
>
This program uses multiple assignments a,b are assigned in the same line

Again you can see that the block starts with “:” and is indented.

 

Lists

Line: 382 to 388
  modules.png
Added:
>
>

Calling functions from a Module

This is how we call a function (method) from a module.

moduleCall.png

... and this is the result:

moduleCallResult.png

Executing Modules

Often modules contain only function or class (see later) definitions

However it is also possible to include the function calls (the main routine)

into the module e.g. to demonstrate how the functions are to be used.

In this case the name of the module changes to __main__

We can therefore find out if the module is imported or executed by checking the name variable:

if __name__ == "__main__"

this executes the main code only if not imported into a main program

Command Line Arguments

Programs can be called with arguments. When calling an editor you my give

the name of the file to be edited as an argument.

There may also be options that are passed as arguments, e.g.

ls -ls $HOME

These arguments can be read (as strings) in Python:

commandLineArgs.png

You may then convert an argument from string to int with

maxFib=int(argv[1])

Running the module with args

idleArgs.png

This code checks if we are running in idle, in which case it asks the user for the arguments.

In case we are called from the command line the arguments are already passed into sys.argv

Files

In order to read of write a file it must first be opened:

f = open(‘filename’,’w’)

After which you can write the file. In read mode, the ‘w’ must be replaced by ‘r’

files.png

Classes

In object oriented programming languages you can define classes

encapsulating data and functions (methods in the oo jargon) that act on these data.

You can create sub-classes having most of the properties of the

base class but provide more data and/or methods or some of the methods are modified (overridden).

This is called inheritance.

Python implements these features.

While in many object oriented languages we distinguish between public and private data,

in Python all data are public.

A convention is to precede a variable name with an ‘_’ if it is not intended for use external to the class.

---+++

Creating Classes

The syntax for creation of classes is similar to functions

createClass.png



i is an instance variable

f is a method

You may define a method with name __init__which is called when

the instance object of the class is created (in the above example: mc = MyClass ()

The short description of the class can be found in the instance variable _ _doc__

Classes and the Sensors

As classes describe real world objects they are particularly using for describing our sensors

Connection details, calibration parameters etc. can be described in the instance variables

Actions of the sensors like triggering, readout etc. are implemented in methods.

Instance initialization

There is a special method named _ _init__ which is called when the instance of a class is created.

This method allows to

  • Initialize instance variables
  • Initialize e.g. registers, addresses and the like
  • Setup I/O for sensors
and much more.

A Class Implementation realizing a simple Calculator

calculatorClass.png

A Sensor Demo

cabling.png

The cobbler PinOut

cobblerPinout.png

The Sensors

This is the idea:

The hardware includes:

  • a color sensor
  • an ultra-sonic distance sensor
  • an rgb LED (not strictly necessary)
We create a virtual world showing the ultra-sonic sensor.

When a colored paper sheet is placed in front of the senors then this sheet

with the right color will be seen in the virtual world.

The distance of the sheet to the sensors is also reflected in the virtual world.

The sensors in the virtual world

We see the distance sensor in front and the color sensor

with its LEDs switched off, in the back

The (white) paper sheet is in frontof the distance sensor

The red ball is the center of the coordinate system

scene-1.png

 %SLIDESHOWEND%

-- Uli Raich - 2018-04-24

Line: 426 to 591
 
META FILEATTACHMENT attachment="cobblerPinout.png" attr="" comment="" date="1524603041" name="cobblerPinout.png" path="cobblerPinout.png" size="288280" user="UliRaich" version="1"
META FILEATTACHMENT attachment="cabling.png" attr="" comment="" date="1524603054" name="cabling.png" path="cabling.png" size="1347268" user="UliRaich" version="1"
META FILEATTACHMENT attachment="twiki.png" attr="" comment="" date="1524641186" name="twiki.png" path="twiki.png" size="382724" user="UliRaich" version="1"
Added:
>
>
META FILEATTACHMENT attachment="commandLineArgs.png" attr="" comment="" date="1524658151" name="commandLineArgs.png" path="commandLineArgs.png" size="6504" user="UliRaich" version="1"
META FILEATTACHMENT attachment="idleArgs.png" attr="" comment="" date="1524658382" name="idleArgs.png" path="idleArgs.png" size="14588" user="UliRaich" version="1"
META FILEATTACHMENT attachment="createClass.png" attr="" comment="" date="1524658840" name="createClass.png" path="createClass.png" size="12041" user="UliRaich" version="1"
META FILEATTACHMENT attachment="calculatorClass.png" attr="" comment="" date="1524659276" name="calculatorClass.png" path="calculatorClass.png" size="43918" user="UliRaich" version="1"
META FILEATTACHMENT attachment="scene-1.png" attr="" comment="" date="1524659680" name="scene-1.png" path="scene-1.png" size="16801" user="UliRaich" version="1"
 
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