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).
◯ ev3 (tank) ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.)
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.
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.
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.
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.
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.
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.
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)!
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