This article is for anyone who wants to work with ev3 in Python. This time, I would like to control the L motor and M motor respectively.
◯ ev3 (intelligent block) ◯ L motor / M motor ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.)
lmotor00.py
#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, OUTPUT_A
L_A = LargeMotor(OUTPUT_A)
while True:
L_A.on(50)
** Point **: The first line starting with #! Is required to execute the program from the intelligent block, so it must be written in every program.
** Point **: How to exit the program ① Press the orange square stop button that appears at the top of the VS Code screen or ② Press the button on the upper left of the intelligent block
** Point **: When programmed with MINDSTORMS, it looks like below.
lmotor01.py
#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, OUTPUT_A
L_A = LargeMotor(OUTPUT_A)
L_A.on_for_seconds(50,3)
** Point **: In this case, rotate at 50 speed for 3 seconds and stop
Point : on_for_seconds(speed, seconds, brake=True, block=True) Rotate the motor at speed for seconds
** Point **: on_for_seconds (speed, number of seconds, ~~) etc. are called functions. A function is like an output device that produces a determined output using the information in (). In this case, the function provided by ev3dev is used according to the rule.
** Point **: When programmed with MINDSTORMS, it looks like below.
lmotor02.py
#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, OUTPUT_A
L_A = LargeMotor(OUTPUT_A)
L_A.on_for_degrees(50,1080)
** Point **: In this case, rotate 1080 degrees at a speed of 50 and stop
Point : on_for_degrees(speed, degrees, brake=True, block=True) Rotate the motor at speed for degrees
** Point **: When programmed with MINDSTORMS, it looks like below.
lmotor03.py
#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, OUTPUT_A
L_A = LargeMotor(OUTPUT_A)
L_A.on_for_rotations(50,5)
** Point **: In this case, make 5 rotations at 50 speed and stop.
Point : on_for_rotations(speed, rotations, brake=True, block=True) Rotate the motor at speed for rotations
** Point **: When programmed with MINDSTORMS, it looks like below.
It just changes to MediumMotor to import. Functions such as on_for_rotaions () can be used in the same way as for L motors.
mmotor00.py
#!/usr/bin/env python3
from ev3dev2.motor import MediumMotor, OUTPUT_A
m_A = MediumMotor(OUTPUT_A)
while True:
m_A.on(50)
** Point **: while ~: is used for iterative processing.
while True:
is a so-called ** infinite loop ** that says "while true".
** Point **: In Python, block processing is specified by indentation. If you want to know more, please check this article because it is easy to understand!
** Point **: Instance
The m_A
in this case is programmatically called a ** instance **.
m_a = mediummotor(output_a)
,Instance generationIt's called.
Reference article about the instance [Introduction to Python] Basic knowledge of classes
** Point **: When programmed with MINDSTORMS, it looks like below.
If you forget how to do it, [the last article I wrote](https://qiita.com/masterkeaton12/items/938457911b0f3f25e161#4%E6%96%B0%E8%A6%8F%E3%83%95%E3 See% 82% A9% E3% 83% AB% E3% 83% 80% E3% 81% AE% E4% BD% 9C% E6% 88% 90)!
Thank you for reading! !! Next time, I would like to write about controlling multiple motors!
I want to make a better article ◯ This is easier to understand ◯ This is difficult to understand ◯ This is wrong ◯ I want you to explain more here We appreciate your opinions and suggestions.
Recommended Posts