This page (revision-7) was last changed on 2025-07-30 13:21 by Murray Altheim

This page was created on 2025-07-30 05:22 by Murray Altheim

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
7 2025-07-30 13:21 2 KB Murray Altheim to previous
6 2025-07-30 13:19 2 KB Murray Altheim to previous | to last
5 2025-07-30 13:17 2 KB Murray Altheim to previous | to last
4 2025-07-30 13:13 2 KB Murray Altheim to previous | to last
3 2025-07-30 12:58 1 KB Murray Altheim to previous | to last
2 2025-07-30 06:43 1 KB Murray Altheim to previous | to last
1 2025-07-30 05:22 1 KB Murray Altheim to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 3 added one line
%%info
At line 5 added one line
%%
At line 5 changed one line
!motor_controller.py
We can define __classes__ in Python. Classes are defined globally, within the file/module as they begin in column 0.
Within a class its functions are called __methods__. Unlike functions, methods are defined within the scope of the class and are therefore indented within the class's block.
A class has a __constructor__, a special method named {{{__init__}}}. The constructor is called when you create a new object of the class. It can be said that the new object has a type of its class.
%%info
All special methods in Python are surrounded by double underscores. (There are more of these but we'll get to them later.)
%%
%%filename boo.py %%
At line 7 changed one line
# motor controller class
class Boo:
__init__(self):
print('boo!')
}}} %%
At line 24 added 32 lines
Now let's enter the Python REPL and use our new class:
%%repl {{{
/pyboard> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.25.0 on 2025-04-15; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>>
>>> from boo import Boo
>>> b = Boo()
boo!
>>>
>>> print(b)
<Boo object at 20005f50>
>>>
>>> print(type(b))
<class 'Boo'>
>>>
}}} %%
%%info
__What did we do?__
After creating a class named {{Boo}},
# we imported the class ("{{from boo import Boo}}") so the interpreter was aware of it,
# then we ''instantiated'' (created an instance of) an object of the Boo class ("{{b = Boo()}}") and we assigned the object a name "b". Notice that when we created the object, the print line in Boo's constructor was executed ("boo!")
# then we printed the object (the interpreter returned a default result "{{<Boo object at 20005f50>}}"),
# then we printed the type of the object ("{{print(type(b))}}")
%%
%%filename motor_controller.py %%
%%python {{{
At line 16 changed 11 lines
print("motor controller ready.")
def set_speed(self, port_speed, stbd_speed):
self.port_forward_motor.set_speed(port_speed)
self.stbd_forward_motor.set_speed(stbd_speed)
def stop(self):
self.port_forward_motor.stop()
self.stbd_forward_motor.stop()
print('motor controller ready.')
At line 30 changed one line
!motor.py:
%%filename motor.py %%
At line 32 removed 3 lines
# this is the motor class
At line 36 removed one line
At line 40 changed 2 lines
print("motor {} ready.".format(self.motor_name))
print('motor {} ready.'.format(self.motor_name))
At line 44 changed one line
print("set motor {} speed to: {}".format(self.motor_name, speed))
print("speed: {}".format(speed))
At line 48 removed 5 lines
def stop(self):
self.motor_speed = 0
print('motor {} stopped.'.format(self.motor_name))
At line 56 changed 2 lines
!main.py:
%%filename main.py %%
At line 59 removed one line
import time
At line 62 removed 2 lines
motor_ctrl = None
At line 68 removed 10 lines
# begin to accelerate in clockwise rotation
for speed in range(0, 101):
motor_ctrl.set_speed(speed,-speed)
time.sleep(3) # wait 3 seconds
# begin to decelerate down to zero (stopped)
for speed in range(100, -1, -1):
motor_ctrl.set_speed(speed,-speed)
At line 81 removed 3 lines
# if motor_ctrl: same thing as:
if motor_ctrl is not None:
motor_ctrl.stop()
At line 99 added 3 lines
----
__Previous:__ [MicroPython Tutorial Exercise 3|MicroPythonTutorialExercise03]: functions \\
__Next:__ [MicroPython Tutorial Exercise 5|MicroPythonTutorialExercise05]: using classes