Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python modules and timing | ||||||||
Line: 43 to 43 | ||||||||
angle = 30 # use 30 degrees as parameter value result = sin_deg(angle) # calculates the sin of "angle" degrees using our own sin_deg function print("sin(",angle,") = ",result) | ||||||||
Added: | ||||||||
> > | Plotting a function | |||||||
Changed: | ||||||||
< < | Let's try these ExerciseSheets#SecondSession Thereafter we will learn how to access hardware in Hardware Access GPIO | |||||||
> > | is the formula for a damped oscillator. In order to plot its shape, we can first define a function which calculates its value for a given x. After that, we call this function for as many points as we want to plot, and we print the x and damped_osc(x) values. from math import exp,sin def damped_osc(x): # calculates the damped # oscillator function return exp(-1/10)*sin(x) for i in range(500): # calculate 500 values print(i/10,damped_osc(i/10) # and print themWhen running the program, we can redirect the printed data to a file. On the PC, using CPython (python3) this is done as follows: python3 damped_osc.py > damped_osc.dat.We can however also run the code on the ESP32 and redirect the output to a file on the PC: ampy run damped_osc.py > damped_osc.datOnce we have the data in a file, we call gnuplot: gnuplot plot "damped_osc.dat" with lines Let's try these ExerciseSheets#SecondSession Thereafter, we will learn how to access hardware in Hardware Access GPIO | |||||||
-- Uli Raich - 2022-10-15
Comments | ||||||||
Added: | ||||||||
> > |
|
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python modules and timing | ||||||||
Line: 27 to 27 | ||||||||
This is how you would go about:
from math import sin,radians print(sin(radians(30))) | ||||||||
Added: | ||||||||
> > | Writing your own functions | |||||||
Changed: | ||||||||
< < | Let's try these. | |||||||
> > | It is easily possible to write your own functions and to create your own modules.
Imagine you want to create a sin function which takes angles in degrees, instead of radians. Let's call the function sin_deg(degrees). It will take its parameter degrees, convert the value to radians and return the sin result, which was calculated with the sin function from the math library. You can define a function with the keyword def. The function body is again indented, like we saw in case of the conditions or the loops:
from math import sin,radians def sin_deg(degrees): rad = radians(degrees) return sin(rad)We can call this function as follows: angle = 30 # use 30 degrees as parameter value result = sin_deg(angle) # calculates the sin of "angle" degrees using our own sin_deg function print("sin(",angle,") = ",result)Let's try these ExerciseSheets#SecondSession Thereafter we will learn how to access hardware in Hardware Access GPIO | |||||||
-- Uli Raich - 2022-10-15 |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python modules and timing | ||||||||
Line: 8 to 8 | ||||||||
| ||||||||
Added: | ||||||||
> > | ||||||||
import utime # import time would work as well for i in range(5): print("Hello World") utime.sleep(1) # delay execution be 1 s, utime.sleep_ms(1000) would do the same. sleep_ms is not available in CPythonThe other option is to import just the function to be used: | ||||||||
Added: | ||||||||
> > | from utime import sleep_ms for i in range(5): print("Hello World!") sleep_ms(1000) # note that utime in front of the sleep call has been omittedIn i similar way, you may use the functions of the math library: In order to calculate sin(30 degrees) you need access to the sin function. The sin function however takes its parameter in radians such that you need the function radians, converting values from degrees to radians, in addition. This is how you would go about: from math import sin,radians print(sin(radians(30))) Let's try these. | |||||||
-- Uli Raich - 2022-10-15 |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Added: | ||||||||
> > |
Python modules and timingPython libraries are arranged as modules, containing a number of functions and/or classes. These modules must be imported before being used. You can either import the complete module, or you can import individual functions or classes to be used. As an example, let's study the time module. In MicrroPython the module is called utime (for micro-time) and it contains functions allowing us to delay execution for a certain amaount of time.
import utime # import time would work as well for i in range(5): print("Hello World") utime.sleep(1) # delay execution be 1 s, utime.sleep_ms(1000) would do the same. sleep_ms is not available in CPythonThe other option is to import just the function to be used: -- Uli Raich - 2022-10-15 Comments |