Aegidius
 Plüss   Aplulogo
     
 www.aplu.ch      Print Text 
© 2021, V10.4
 
  RaspiBrickPi Library
 
Motors and Gear
 

 

The following examples are based on the standard rover assembly as shown before using two EV3 motors connected to port MA and MB (NXT motors may also be used). The motors have a build-in encoder that reports the current rotating position, but there is no feedback regulation algorithm included in the library. So the motor speed depends somewhat on the current battery voltage.

Single motor moving forward for a given time

from ev3brickpi import *

robot = LegoRobot()
motA = Motor(MotorPort.A)
oled = OLED1306()

oled.println("forward")
motA.forward()
Tools.delay(2000)
oled.println("speed = 70")
motA.setSpeed(70)
Tools.delay(2000)
oled.println("forward")
motA.forward()
Tools.delay(2000)
oled.println("done")
Tools.delay(1000)
robot.exit()

The forward()/backward() methods return immediately while the motor continues to run. So we may think that these commands "put the motor in a forward/backward-state". The exit() command stops all running motors automatically.

There are several methods in the Motor class that uses the rotation counter, among them getMotorCount(), rotateTo(), continueTo(), continueRelativeTo().

 

Two motors moving together

To handle the moving rover with ease, a class Gear is defined that combines two motors at port MA and MB. To most simple action is to drive the rover forward and then backward a while.

from ev3brickpi import *

robot = LegoRobot()
gear = Gear()
oled = OLED1306()

oled.println("Running forward")
gear.forward()
Tools.delay(2000)
oled.println("Stopping")
gear.stop()
Tools.delay(2000)
oled.println("Running backward")
gear.backward()
Tools.delay(2000)
gear.stop()
robot.exit()

The motors are not really synchronized using the rotation counter. So if some deviation of the straight motion may result. The methods left() and right() turn one wheel forward and the other backward and the rover rotates at the same spot. For turning smoothly in the forward or backward direction, the methods leftArc() and rightArc() are used.

There are overloaded versions of these methods with a time parameter. These are blocking functions that returns only at the end of the time interval. If within a short time delay, no other moving command is issued, the gear stops, otherwise there is no stop between consecutive movements. This feature ensures that the movement is smooth with no jerks.

from ev3brickpi import *

robot = LegoRobot()
gear = Gear()
gear.setSpeed(40)
gear.forward(3000)
gear.leftArc(0.5, 3000)
gear.forward(3000)
gear.rightArc(0.2, 3000)
robot.exit()

This is the most simple program to move the rover and it shows the strength of the object oriented design.