Python for SPIKE Prime (LEGO 45678) Part 6: Turns with Functions ***For version 2 (Legacy)***

Creator Academy Australia · Beginner ·🧒 Coding for Kids ·5y ago

Key Takeaways

Learn Python functions for accurate gyro turns with LEGO SPIKE Prime

Full Transcript

hello welcome to another episode of python for spike prime today we're going to learn about using functions to make your robot do really accurate turns hello my name is gary and today we are going to look at uh using function so it's going to be a little bit more complicated than the previous episodes so if you haven't already then check out the previous episodes over here today we're going to look at defining functions which in scratch equates to something like my blocks my blocks are really useful in scratch because it lets you repeat things that you um you don't want to have to retype or or copy a lot of times so functions are really really important for programming in general let's jump right in first of all let's um open up spike prime start a new project uh call it ca python functions and then hit the python icon click create we'll hide the helper okay so here we are importing all our classes then we create objects and then we run the code so here let's do some hints just for our own reference because there's going to be a bit of a change later on first thing we want to do is define our motors so uh let's change it up a bit let's call it wheels equals motor pair c d remember um these objects we can call them whatever we want so um wheels motor movement motors they're all they're all fine as long as you keep consistent all through your code and then the way that we use the the gyro to turn on a robot is that we use a um the internal gyro on the spike prime hub there's no separate gyro sensor for spike prime uh it's all built into the hub and the um the the sensor is called a motion sensor inside the python language when we want to turn a certain angle for example if we want to turn the robot 90 degrees a lot of people who start off with with coding with lego um get get confused they use the move degrees command so they might do something like um [Music] say i want a moving degree so let's go wheels dot move and then let's go like 90 degrees right that looks looks correct but it's actually not because um when you put in 90 degrees like that the degrees is actually the amount that the wheels are rotating okay so if the wheels rotate 90 degrees it's like a quarter of a rotation and that's not going to be enough to turn your robot all the way around to 90 degrees so uh it's a little bit uh a bit tricky for for new programmers to understand that that the 90 degrees on the movement wheels uh only change the degrees of the movement not the actual whole angle of the robot does that make sense in order to do that we need to do this so we go while hub dot motion sensor um dot get your angle is less than 90 then so this is a while loop and we're doing we're repeating something until the your angle is no longer less than 90 degrees what are we repeating we're repeating the wheels movement wheels dot start a hundred so when you go start then the value in here is a steering value remember it defaults at 0 which is a straight move but 100 is turned towards the right and negative 100 is turned towards the left and then when we reach um 90 degrees then what do we want to do wheels don't stop okay super simple but there's a there was a lot to unpack in there right let's uh connect our robot and try to get it working okay right connected let's run the code and see if we do a 90 degree turn cool so that works it was a little bit quick so let's change the speed down a little bit wheels that set default speed to say 20. all right but now what if i wanted to repeat right hand turns throughout my code i don't want to have to copy this out all the time so now we're going to create a function so to create a function the proper way to do it would be to do it after you create objects but before you execute the code so here we write down functions and to define a function we use the def keyword so def we'll define define right turn open and close brackets and then column and then here this is where we define the function and then now we're going to copy well cut all this code out and then paste it in here but we've got to make sure that everything is indented to be under the right hand turn function this wheel start has to be indented under the while loop okay so the indentations in python are really important because it tells the code what the scope is okay so now now that we've defined the function for a right turn we've got to call the function so down here where we had the code before we just go right turn open and close bracket okay let's run the code [Music] perfect what if i tried to run the code twice if i wanted to run the code twice then you'd think that i'd go like this right turn right turn again but you're going to find that something strange is going to happen so then you did the turn once and the reason is because when you start the code it resets the gyro um and then it doesn't reset the gyro after you've made the turn so it's a good thing we have a function because now we're going to use the function to reset the gyro before we do the maneuver okay so let's have a look at this so here we want to reset the gyro before the start of moving on our turn so we're going to define our function again we go hub.motionsensor dot reset your angle okay so now it should be fine and do the two moves instead of just the one [Music] okay perfect so that's how we do a right-hand turn but what if i want to do a custom angle term just like in scratch we can add parameters to our my blocks right and here we add parameters to our functions arguments so um we're going to add a um a parameter in so that you can change what degrees you want to turn the robot [Music] here we go so we don't have to worry about the right hand turns and we can keep our function there because you know you might want to do a right-hand turn like that so let's go define custom turn and then here we put an angle angle is a um is a is a um a variable that we create right that's this could be named anything it could be x for example but um here we're going to call it angle and then everything is going to be exactly the same as what we had before except this 90 instead of 90 we're going to replace that with angle so now if i wanted to do a custom turn then i would say like if i want to do a 45 degree turn then i'd go custom turn 45 okay [Music] okay isn't that cool all right that was a really uh super simple short and um also quite complex uh lesson so i hope you enjoyed it please like and subscribe if it's helping you out and i will see you again on the next one bye you

Original Description

***For version 2 (Legacy)*** Learn how to code in Python for the LEGO Mindstorms Robot Inventor set (51515). Learn how to do another essential robotics skill: accurate gyro turns with Python. In this lesson: - gyro assisted turning - functions - function parameters Be sure to subscribe for more on this series! To do the same lesson for SPIKE Prime, see this video: https://youtu.be/gBgAeASYkWU
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Creator Academy Australia · Creator Academy Australia · 0 of 60

← Previous Next →
1 LEGO Wedo 2.0 Kangaroo and Joey Demonstration
LEGO Wedo 2.0 Kangaroo and Joey Demonstration
Creator Academy Australia
2 LEGO Wedo 2.0 Koala Demonstration
LEGO Wedo 2.0 Koala Demonstration
Creator Academy Australia
3 LEGO WeDo 2.0 Eagle vs Rabbit Demonstration
LEGO WeDo 2.0 Eagle vs Rabbit Demonstration
Creator Academy Australia
4 Animatronic Australia: The Kangaroo
Animatronic Australia: The Kangaroo
Creator Academy Australia
5 How Semiconductors Work: 3D Animation
How Semiconductors Work: 3D Animation
Creator Academy Australia
6 LEGO Wedo 2.0 Koala
LEGO Wedo 2.0 Koala
Creator Academy Australia
7 First impressions and unboxing First Lego League: Into Orbit 2018
First impressions and unboxing First Lego League: Into Orbit 2018
Creator Academy Australia
8 FLL Into Orbit Team Ranking Gears talks about Artificial Gravity
FLL Into Orbit Team Ranking Gears talks about Artificial Gravity
Creator Academy Australia
9 FLL Into Orbit team Ev3lution discusses isolation in space
FLL Into Orbit team Ev3lution discusses isolation in space
Creator Academy Australia
10 PI3-BOT (Pie Bot) Prototype
PI3-BOT (Pie Bot) Prototype
Creator Academy Australia
11 Dynamic display stand for LEGO Bugatti Chiron (preview)
Dynamic display stand for LEGO Bugatti Chiron (preview)
Creator Academy Australia
12 Creative Kids TV announcement featuring Creator Academy
Creative Kids TV announcement featuring Creator Academy
Creator Academy Australia
13 LEGO WeDo 2.0 Guided Lesson: Milo
LEGO WeDo 2.0 Guided Lesson: Milo
Creator Academy Australia
14 LEGO SPIKE PRIME: Turtle Prime Demonstration
LEGO SPIKE PRIME: Turtle Prime Demonstration
Creator Academy Australia
15 LEGO SPIKE PRIME "Wheelchair Prime"
LEGO SPIKE PRIME "Wheelchair Prime"
Creator Academy Australia
16 10 Reasons why EV3 is SUPERIOR to SPIKE Prime
10 Reasons why EV3 is SUPERIOR to SPIKE Prime
Creator Academy Australia
17 Your first launch after installing Python: Python for Kids Webinar Highlight
Your first launch after installing Python: Python for Kids Webinar Highlight
Creator Academy Australia
18 What is "Sharpening the Saw": Python for Kids Webinar Highlight
What is "Sharpening the Saw": Python for Kids Webinar Highlight
Creator Academy Australia
19 Why We Use Comments: Python for Kids Webinar Highlight
Why We Use Comments: Python for Kids Webinar Highlight
Creator Academy Australia
20 Build a Scratch Dinosaur Park (Full Lesson)
Build a Scratch Dinosaur Park (Full Lesson)
Creator Academy Australia
21 Scratch 3.0: Cat and Mouse Game Stream (Krunal)
Scratch 3.0: Cat and Mouse Game Stream (Krunal)
Creator Academy Australia
22 Python Tutorial: Make a Toilet Paper Calculator (Full Lesson)
Python Tutorial: Make a Toilet Paper Calculator (Full Lesson)
Creator Academy Australia
23 Scratch 3.0 Tutorial: Making a chatbot that plays 'heads or tails' (Full Lesson)
Scratch 3.0 Tutorial: Making a chatbot that plays 'heads or tails' (Full Lesson)
Creator Academy Australia
24 Getting Started with MicroPython on SPIKE PRIME (old)
Getting Started with MicroPython on SPIKE PRIME (old)
Creator Academy Australia
25 Easter Egg Game Tutorial in Scratch (Full Lesson)
Easter Egg Game Tutorial in Scratch (Full Lesson)
Creator Academy Australia
26 Accessing Weather API with Python (Highlights)
Accessing Weather API with Python (Highlights)
Creator Academy Australia
27 Accessing Translate API with Python (Highlights)
Accessing Translate API with Python (Highlights)
Creator Academy Australia
28 Scissors, Paper, Rock with Scratch 3.0 (Highlights)
Scissors, Paper, Rock with Scratch 3.0 (Highlights)
Creator Academy Australia
29 Making a really scary game with Scratch 3.0 (Highlights)
Making a really scary game with Scratch 3.0 (Highlights)
Creator Academy Australia
30 Making a Lemmings game with Scratch 3.0 (Highlights)
Making a Lemmings game with Scratch 3.0 (Highlights)
Creator Academy Australia
31 Space Navigation Game in Scratch (Highlights)
Space Navigation Game in Scratch (Highlights)
Creator Academy Australia
32 Scratch Supercat Game (Full Lesson)
Scratch Supercat Game (Full Lesson)
Creator Academy Australia
33 Introduction to Robots with LEGO Wedo 2.0 (Full Lesson)
Introduction to Robots with LEGO Wedo 2.0 (Full Lesson)
Creator Academy Australia
34 Getting Started With LEGO Spike Prime (Full Lesson)
Getting Started With LEGO Spike Prime (Full Lesson)
Creator Academy Australia
35 Scratch Walk Cycle Animation Tutorial (Full Lesson)
Scratch Walk Cycle Animation Tutorial (Full Lesson)
Creator Academy Australia
36 Building a Scissor Lift with LEGO Simple Machines (Highlights)
Building a Scissor Lift with LEGO Simple Machines (Highlights)
Creator Academy Australia
37 EV3 datalogging RPM challenge (highlights)
EV3 datalogging RPM challenge (highlights)
Creator Academy Australia
38 Learning about buses with LEGO (Full Lesson)
Learning about buses with LEGO (Full Lesson)
Creator Academy Australia
39 Make "Flappy Bird" in Scratch 3.0 (Highlights)
Make "Flappy Bird" in Scratch 3.0 (Highlights)
Creator Academy Australia
40 "Memory Arrows" game with Scratch 3.0 Lists (Highlights)
"Memory Arrows" game with Scratch 3.0 Lists (Highlights)
Creator Academy Australia
41 Learning about Gear Ratios with LEGO Simple Machines (Highlights)
Learning about Gear Ratios with LEGO Simple Machines (Highlights)
Creator Academy Australia
42 Scratch 3.0 Bouncing Ball with Stretch, Speed and Acceleration (Full Lesson)
Scratch 3.0 Bouncing Ball with Stretch, Speed and Acceleration (Full Lesson)
Creator Academy Australia
43 LEGO SPIKE Prime Order Tracking Robot (Highlights)
LEGO SPIKE Prime Order Tracking Robot (Highlights)
Creator Academy Australia
44 Scratch 3.0 Race Guesser (Full Lesson)
Scratch 3.0 Race Guesser (Full Lesson)
Creator Academy Australia
45 How Banks Work - a quick explanation for kids
How Banks Work - a quick explanation for kids
Creator Academy Australia
46 SPIKE Prime Safe + importance of passwords (Highlights)
SPIKE Prime Safe + importance of passwords (Highlights)
Creator Academy Australia
47 Combination Safe Coding Guide with LEGO SPIKE Prime
Combination Safe Coding Guide with LEGO SPIKE Prime
Creator Academy Australia
48 LEGO SPIKE Prime Parcel Scanning Robot
LEGO SPIKE Prime Parcel Scanning Robot
Creator Academy Australia
49 Building "Addy" the Addition Chatbot
Building "Addy" the Addition Chatbot
Creator Academy Australia
50 SPIKE 2.x PID Balancer with Scratch for SPIKE Prime / 51515: Gyro Girl
SPIKE 2.x PID Balancer with Scratch for SPIKE Prime / 51515: Gyro Girl
Creator Academy Australia
51 What is Procedural Programming
What is Procedural Programming
Creator Academy Australia
52 Zombie game in SCRATCH (plus horrifying zombie facts!)
Zombie game in SCRATCH (plus horrifying zombie facts!)
Creator Academy Australia
53 Swarming Bugs Scratch Game
Swarming Bugs Scratch Game
Creator Academy Australia
54 LEGO Mindstorms/SPIKE Prime Scanner and Printer
LEGO Mindstorms/SPIKE Prime Scanner and Printer
Creator Academy Australia
55 Fishing Game with SCRATCH (For Fathers Day!)
Fishing Game with SCRATCH (For Fathers Day!)
Creator Academy Australia
56 Measuring Paper Plane Speed with SPIKE Prime Line Graphs
Measuring Paper Plane Speed with SPIKE Prime Line Graphs
Creator Academy Australia
57 Comparing Mindstorms Robot Inventor and SPIKE Prime
Comparing Mindstorms Robot Inventor and SPIKE Prime
Creator Academy Australia
58 Code a 'Knock Knock Joke' game in Scratch 3.0
Code a 'Knock Knock Joke' game in Scratch 3.0
Creator Academy Australia
59 Creator Academy Australia Live Stream
Creator Academy Australia Live Stream
Creator Academy Australia
60 Discovering LEGO Mindstorms/SPIKE Gyro Pitch Deadzone
Discovering LEGO Mindstorms/SPIKE Gyro Pitch Deadzone
Creator Academy Australia

Related Reads

📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Learn how creative characters like Sprunki inspire kids to learn coding through interactive games and animations
Medium · Programming
📰
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Learn how to introduce primary school children to computer applications in a fun and safe way, teaching them essential skills for the future.
Dev.to · Ogunkola Adeola
📰
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Create an interactive coding book for kids using Rust, with a focus on storytelling and bilingual support, to teach programming concepts in an engaging way
Dev.to · born1987-ir
📰
How Kids Can Build Fighting Games Stickman in Scratch
Kids can learn coding by building a stickman fighting game in Scratch, developing skills like movement control and collision detection.
Medium · Programming
Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →