Difference: ModulesAndTiming (3 vs. 4)

Revision 42022-10-23 - UliRaich

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

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

>
>
damped_osc.png
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 them

When 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.dat

Once we have the data in a file, we call gnuplot:

gnuplot
plot "damped_osc.dat" with lines

damped_osc_plot.png

 

Let's try these ExerciseSheets#SecondSession
Thereafter, we will learn how to access hardware in Hardware Access GPIO

  -- Uli Raich - 2022-10-15

Comments

<--/commentPlugin-->
Added:
>
>
META FILEATTACHMENT attachment="damped_osc.png" attr="" comment="" date="1666552821" name="damped_osc.png" path="damped_osc.png" size="3331" user="UliRaich" version="1"
META FILEATTACHMENT attachment="damped_osc_plot.png" attr="" comment="" date="1666553610" name="damped_osc_plot.png" path="damped_osc_plot.png" size="39392" 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