0% found this document useful (0 votes)
161 views43 pages

Spike Prime - Python Coding - Sample

Uploaded by

tamimtariq2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views43 pages

Spike Prime - Python Coding - Sample

Uploaded by

tamimtariq2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Spike Prime

The state of being expert


Contents

01 …………………………………………… Introduction
02 …………………………………………… Utilizing The Hub
03 …………………………………………… Conditional Statements
04 …………………………………………… Straight Movement & Relative Position
05 …………………………………………… Moving an Arm
06 …………………………………………… Building Custom Functions
07 …………………………………………… Rotation around Z-axis (The Motion Sensor)
08 …………………………………………… Utilizing External Sensors
09 …………………………………………… The Concept of Variables
Introduction
Technical Jargon
modules: are simply files with the ".py" extension containing Python code that can be imported inside
another Python Program. In simple terms, we can consider a module as a code library.

routine or subroutine or subprogram: it’s a piece of code (method) used to execute repetitive tasks,
such as saving a file. The nature of execution is sequence (in order).

coroutine (cooperative routine): it’s a term that describes a method that is executed with the
cooperation with another methods/s through the ability of being suspended and resumed during its
execution, which gives the effect of (multithreading technique). The following points distinguishes
coroutines from multithreads:
• coroutines are executed within a thread
• coroutines can switch their context (thread)
• coroutines are suspendable
Static import
import hub: to import the entire module (hub), and creates a reference to that module in the current
project you are working on. Then, to access a particular attribute or method from inside that module
(e.g. hub.method or hub.attribute); you need to define a completed module path.

from hub import light_matrix: to import only a specified section (e.g. classes or methods ) from the
module (hub) which is the (light_matrix). This helps in preventing namespace conflicts and reduce
memory usage.

from hub import light_matrix as lm: to create an alias for an imported module (Aliasing technique ), this
allows you to give a different name to the module you’re importing. This helps to shorten long or
complex names for modules and to avoid conflicting of similar names of ( public methods and variables)
from different imported modules.
Notes:
• from, import, and as are built-in methods.
• "import" statements should be written at the top beginning of the code.
Troubleshooting Python’s modules Import Issues:

1. The "ModuleNotFoundError" problem:


This Occurs when Python can’t locate the module you’re trying to import, usually, because we’re
trying to import a module that doesn’t exist. The solution it to make sure the module you’re trying to
import exists and is in the correct location. Check your spelling and capitalization.
2. The "Navigating Circular Imports" problem :
This occurs when two or more modules depend on each other, either directly or indirectly. This can
lead to infinite loops and other unexpected behavior. The solution is often to refactor your code to
remove the circular dependency; this might involve moving some code to a separate module or
changing the order of your imports.
3. The "Import in Larger Projects" problem:
In larger projects, only import what you need and organize your Imports by placing all import
statements at the top of the file and separating the deferent types of imported modules with a blank
line.
Utilizing The Hub
Modules

hub

Sub Modules

light_matrix sound light button port


Methods of the hub module
1. To get the (uuid) of your robot’s hub, use device_uuid()
2. To get the (id) of your robot’s hub, use hardware_id()
3. To programmatically turn off the hub, use power_off()
4. To get the hub temperature in deci-degrees Celsius (d°C),
use temperature()
All the previous methods are not awaitable and (ND).
Ex.1 Ex.2
The 5 × 5 Light Matrix / sub module: light_matrix
1. To display one of the built in images, use
show_image(light_matrix.IMAGE_HAPPY). By changing
the attribute (IMAGE_HAPPY) you can change the x: 0 to 4
displayed image.
2. To displays text on the Light Matrix, one character at a
time, use write("hi")
3. To power up a certain pixel, use set_pixel (y,x,brightness)
4. To turn off all the pixels, use clear()
y: 0 to 4
All the previous methods are not awaitable expect the "write"
and all of them are (ND).
Ex.1 Ex.2

We used the “sleep” method from the general


module “time”
Ex.3 ND method : it’s a method that the compiler doesn’t
await until it finishes; instead, after triggering that
method, the compiler skips it and operates the next line
of code in parallel. This may cause:
- The advantage of parallel orders "Parallel operation of
some lines of code"
- The problem of conflicting parallel orders (if the
orders are conflicting)

Conflicting parallel orders problem appears here.


Ex.4 When we have two parallel conflicting methods (orders)
– the 1st one is ND – , we need to force the compiler to
await the 1st one until it finishes, this can be done by
using a suspension tool such as:
1. The (sleep) method
2. Looping statements (e.g. while)
3. The concept of "await and async"

The problem is solved by using the (sleep)


method
Making Sounds / sub module: sound
1. To make a beep, use beep(freq, duration, volume) The
volume ranges from 0 to 100 and the duration in ms.
This is an awaitable and (ND) method.
2. To stop all sounds from the hub, use stop() , it’s not
awaitable and (ND) method.
Ex.1 Ex.2

These are ND methods, conflicting orders


problem appears here.
Ex.3 Ex.4

The problem is solved by using the (sleep) Parallel methods (orders) – no conflicting
method
Changing the power button color / sub module: light
1. To change the power button color, use color(0,9),
The argument (0) indicates the power button, the
second argument indicates the color (it ranges from
0 to 10). This is not awaitable and (ND) method.
Ex.1 Ex.2
Using the Button interface / sub module: button
1. To check whether the left/right button is
being pressed, use pressed(button.LEFT),
the argument (button.LEFT) indicates the
left button and (button.RIGHT) indicates
the right button. This is not awaitable
and (ND) method.
Ex.1
Using the port interface / sub module: port
1. This module contains constants (attributes) that
enables easy access to the ports on the hub. (port.A,
port.B, .... port.F), you can replace the letters with
numbers (port.0, port.1, .... port.5).
Actually, this sub module is used when you connect an
electrical device to the hub.
Ex.1
Modules
device_uuid()

hardware_id() hub
power_off()
temperature()
Sub Modules

light_matrix sound light button port

show_image() beep() color() pressed() port.A


write() stop() port.B
clear()

set_pixel()
Conditional Statements
The "If" statement and the "while" loop
Ex.2 Forever loop
Ex.1 Mere if statement

"sleep()" is (D) and not awaitable Did you notice the effect ?
Did you notice any effect ?
Ex.5 Repeat n times
The "for" loop

Ex.1 Repeat n times Ex.2 Repeat n times


The "break" statement

Ex.1 End the forever loop Ex.2 End the forever loop

Nothing after line-9, so the program ends The program continues after line-9
there (at "break" )
Straight movement & Relative Position
Modules

hub
motor_pair motor

port
Move tank; module: motor_pair , motor / sub module: port

1. To move for certain degrees, use move_tank_for_degrees(pair_slot, degrees , left_velocity,


right_velocity, *stopping mode). This method is awaitable and (ND).

2. To move for certain seconds, use move_tank_for_time(pair_slot, left_velocity, right_velocity,


duration"ms", *stopping mode). This method is awaitable and (ND).

3. To move without limitations, use move_tank(pair_slot, left_velocity, right_velocity). This method is


not awaitable and (ND).
Ex.1 Ex.2

The (move_for_degrees) method is ND


Relative Position; module: motor / sub module: port

Ex.1 Using the "sleep" method


Relative Position as a suspension tool:
When it comes to the methods of "Straight
Movement" , there are two suitable suspension
tools that can be used:
- The "sleep" method.
- A looping statement "while" with the "relative
position" property.

We compelled the compiler to await (move_for_time)


method.
Relative Position as a distance identifier:
When you asked to move your robot to exactly 1m, you need a method to specify how many degrees must
be moved to travel 1m, this depends on :
- The circumference of the wheels
- The concept of relative position

This wheel has a circumference of 17.5 cm, so:


Each 1 rotation (360°) The robot travels 17cm
X The robot travels 100cm

X = 2057° , so if the motors rotate 2057°, the robot "as a body" travels
1m in real.
Modules

motor_pair motor hub

pair() motor.BRAKE
port
motor.COAST
unpair()

motor.HOLD port.A
move_for_degrees() move_tank_for_degrees()
reset_relative_position() port.B
move_for_time() move_tank_for_degrees()
relative_position()
move() move_tank()

stop()
Mission 1: the robot moves 1.7m back and forth 3 times, then stops and still waiting until the (left
button) is pressed to exert a sound for (2 sec) and exit the program.

Mission 2: the robot keeps waiting, if the (right button) is pressed, it consistently moves forward,
if the (left button) is pressed, it consistently moves backward, the robot must stop when the (right
button) is pressed again (two times).

Mission 3: (ready, steady, go! ) the 1st press on the (left button) shows (ready), and so on, after
the 3rd press the robot moves forward for 3 sec, then the program keeps repeating itself until the
(right button) is pressed.

Mission 4: write a code that receives the (time) and (velocity), calculates and shows the (expected
traveled back and forth distance), and then it goes the distance back and forth .
The Concept of Variables
Preamble
a Variable in programming is something like a storage case, data (numbers, symbols, letters, words, …) can be
stored within. a Type of a variable is determined by the type of data that is store within.

We have 3
variables, there
types are
respectively;
int, char, string
Ex.3 the robot must stop when the detected Ex.4 The robot goes off when the detected color
color changes. changes, until it changes again the robot stops.

The problem is no enough period of time


between the two identical commands.
Mission 7: Two objects are placed randomly (random places and ordering), the robot should pick up and
move each one to its corresponding area.

0.3 m
0.75 m 0.5 m

Mission 8: Use some pervious versions of WRO to choose some missions to solve .

You might also like