[ev3dev × Python] Control of multiple motors

This article is for anyone who wants to work with ev3 in Python. This time, I would like to control multiple motors to run the tank (car).

table of contents

  1. What to prepare
  2. Tank
  3. Steering
  4. Program example
  5. About program execution

0. What to prepare

◯ ev3 (tank) ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.)

1. Tank

1-0. Tank Basic Program (Document p.26)

tank00.py


#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_B, OUTPUT_C, MoveTank

tank_drive = MoveTank(OUTPUT_B,OUTPUT_C)

tank_drive.on_for_rotations(80,-30,6)

Point : class ev3dev2.motor.MoveTank(left_motor_port, right_motor_port, desc=None, motor_class=<class ’ev3dev2.motor.LargeMotor’>)

** Point **: Enter the speed of each motor to determine the direction of travel.

** Point **: When programmed with MINDSTORMS, it looks like below. スクリーンショット 2020-06-16 10.42.35.png

2. Steering

2-0. Basic Steering Program (Document p.29)

steering00.py


#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A, OUTPUT_B, MoveSteering, SpeedPercent

steering_drive = MoveSteering(OUTPUT_A,OUTPUT_B)

steering_drive.on_for_rotations(-10,SpeedPercent(75),5)

Point : class ev3dev2.motor.MoveSteering(left_motor_port, right_motor_port, desc=None, motor_class=<class ’ev3dev2.motor.LargeMotor’>)

** Point **: For tanks (cars) only. Image close to steering wheel operation.

** Point **: The degree of bending can be directly determined by one numerical value.

** Point **: ** Steering value **

0 <Steering value <= 100: Turn to the right. Spin at 100 (turn right on the spot). Steering value = 0: Go straight -100 <= Steering value <0: Turn to the left. Spin at -100 (turn left on the spot)

** Point **: When programmed with MINDSTORMS, it looks like below. スクリーンショット 2020-06-16 9.03.53.png

3. Program example

3-0. A program that increases the steering value by 10.

example00.py


#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveSteering,SpeedPercent

steering_drive = MoveSteering(OUTPUT_A,OUTPUT_B)

for i in range(0,101,10):
    steering_drive.on_for_seconds(i,SpeedPercent(75),1)
steering_drive.stop()

** Point **: Increase i by 10 within the range of 0 <= i <101 In other words 1st loop (i = 0): The car goes straight Second loop (i = 10): The car turns a little to the right  : 11th loop (i = 100): The car turns clockwise on the spot.

** Point **: For loops, this article is easy to understand.

3-1. A program that randomly determines the direction of travel

example01.py


#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveSteering,SpeedPercent
import random

steering_drive = MoveSteering(OUTPUT_A,OUTPUT_B)

for i in range(10):
    num = random.randint(-100,100)
    print(num)
    steering_drive.on_for_seconds(num,SpeedPercent(75),2)
steering_drive.stop()

** Point **: Randomly decides the direction of travel every 2 seconds

Point : random.randint(min,max) Randomly retrieves an int value (integer value) from the specified range (min ~ max).

** Point **: For random functions, this article is easy to understand.

3-2. A program that changes the rotation speed of the motor using a list

example02.py


#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveTank
from time import sleep

tank_drive = MoveTank(OUTPUT_A,OUTPUT_B)
speed_list = [-90,30,-10,80,10]

for i in speed_list:
    sleep(1)
    tank_drive.on_for_rotations(i,i,2)
tank_drive.stop()

** Point **: Take numbers from the top of the list and set them to the speed of each motor.

Point : sleep() Function to wait for the number of seconds specified by the argument

** Point **: For the list, this article is easy to understand.

3-3. A program that changes the rotation speed of a motor by combining a list and a random function.

example03.py


#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveTank
from time import sleep
import random

tank_drive = MoveTank(OUTPUT_A,OUTPUT_B)
speed_list = [-90,30,-10,80,0]

for i in speed_list:
    sleep(1)
    num = random.choice(speed_list)
    tank_drive.on_for_rotations(num,0,2)
tank_drive.stop()

** Point **: Randomly pick a number from the list and set it to the speed of one motor (port A) 8 times

Point : random.choice() It takes an object with multiple elements such as a list or a character string as an argument, and randomly returns (outputs) one element from it.

** Point **: For randomness, this article is also easy to understand.

4. About program execution

If you forget how to do it, [previously written article](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'll write about displays, sounds, and lights!

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] Control of multiple motors
Install multiple versions of Python
[ev3dev × Python] Single motor control
[ev3dev × Python] Display, audio, LED control
[Python] Chapter 05-02 Control Syntax (Combination of Conditions)
[ev3dev × Python] SSH Control (remote control with keyboard)
Introduction of Python
Basics of Python ①
Basics of python ①
Copy of python
Install Python Control
Introduction of Python
[Python of Hikari-] Chapter 05-06 Control Syntax (Basics of Comprehension)
[Python] Takes representative values ​​of multiple images [Numpy]
Use multiple versions of python environment with pyenv
Let's control EV3 motors and sensors with Python
[Python] Operation of enumerate
List of python modules
Multiple inheritance of classes
[ev3dev × Python] Ultrasonic sensor
[Python of Hikari-] Chapter 05-08 Control syntax (while statement-another iterative syntax-)
Unification of Python environment
Copy of multiple List
Copy of python preferences
[Python] Create multiple directories
Basics of Python scraping basics
[python] behavior of argmax
Usage of Python locals ()
the zen of Python
Perform multiple version control of Go with anyenv + goenv
[ev3dev × Python] Touch sensor
Installation of Python 3.3 rc1
[ev3dev × Python] Gyro sensor
Python control syntax (memories)
# 4 [python] Basics of functions
[Ev3dev] Let's understand the mechanism of LCD (screen) control
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
[ev3dev × Python] Color sensor
Basics of python: Output
[Python of Hikari-] Chapter 05-05 Control syntax (for statement-multiple loops-)
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Various processing of Python
Version control of Node, Ruby and Python with anyenv
Using multiple versions of Python on Mac OS X (2) Usage
[Python of Hikari-] Chapter 05-10 Control syntax (interruption and continuation of iteration)
[Python of Hikari-] Chapter 05-04 Control syntax (for statement-use of range function-)
[Python of Hikari-] Chapter 05-07 Control syntax (conditional branching of comprehension notation)
Sum of multiple numpy arrays (sum)
[Python] Correct usage of map
Towards the retirement of Python2
Summary of python file operations
[ev3dev × Python] Intelligent block button
Summary of Python3 list operations
Python --Quick start of logging
Recommendation of binpacking library of python
[Python tutorial] Control structure tool
Catch multiple types of exceptions
[python] Value of function object (?)