[ev3dev × Python] Single motor control

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.

table of contents

  1. What to prepare
  2. L motor control
  3. Control of M motor
  4. About program execution

0. What to prepare

◯ ev3 (intelligent block) ◯ L motor / M motor ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.)

1. L motor control

1-0. Program to rotate the L motor all the time

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. スクリーンショット 2020-06-15 18.41.03.png

1-1. A program that rotates the L motor for a certain period of time

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. スクリーンショット 2020-06-15 20.16.50.png

1-2. Program to rotate the L motor by a certain angle

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. スクリーンショット 2020-06-15 20.29.16.png

1-3. A program that rotates the L motor a certain number of times

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. スクリーンショット 2020-06-15 20.37.15.png

2. Control of M motor

It just changes to MediumMotor to import. Functions such as on_for_rotaions () can be used in the same way as for L motors.

2-0. Program to rotate the M motor all the time

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. スクリーンショット 2020-06-15 18.04.27.png

3. About program execution

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)!

Finally

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

[ev3dev × Python] Single motor control
[ev3dev × Python] Control of multiple motors
[ev3dev × Python] Display, audio, LED control
Install Python Control
[ev3dev × Python] SSH Control (remote control with keyboard)
Why you are interested in motor control in Python
[ev3dev × Python] Ultrasonic sensor
[ev3dev × Python] Touch sensor
[ev3dev × Python] Gyro sensor
[ev3dev × Python] Color sensor
[ev3dev × Python] Intelligent block button
Instrument control using Python [pyvisa]
[ev3dev × Python] Build ev3dev development environment
Control the motor with a motor driver using python on Raspberry Pi 3!
Python control syntax, functions (Python learning memo ②)
Virtual Environment Version Control Summary Python
[Raspberry Pi] Stepping motor control with Raspberry Pi
Servo motor control with Raspberry Pi
Study from Python Hour2: Control statements
Python> list> Convert double list to single list
Try frequency control simulation with Python
Python> tuple> Convert double tuple to single tuple
[Ev3dev] Let's make a remote control program by Python with RPyC protocol