Skip to content

Instantly share code, notes, and snippets.

@attilavago
Last active January 1, 2017 22:30
Show Gist options
  • Save attilavago/409df8810652890dc55f85fca699c638 to your computer and use it in GitHub Desktop.
Save attilavago/409df8810652890dc55f85fca699c638 to your computer and use it in GitHub Desktop.
Running motors with robotary
// Test the motors all all 4 ports
import RobotKit
// Create a new EV3Robot instance to talk to the robot
let robot = try EV3Robot()
// Access motors at ports A, B, C and D.
let motors = robot.motors(atPorts: [.A, .B, .C, .D]) // note de dot notation of the ports
// Adding variables to access motors individually
let motorA = robot.motors(atPorts: [.A])
let motorB = robot.motors(atPorts: [.B])
let motorC = robot.motors(atPorts: [.C])
let motorD = robot.motors(atPorts: [.D])
print("waiting for 5 seconds")
wait(seconds: 5) // setting an initial timeout of 5 seconds
// All motors drive forward in the current direction at 40% speed for 2 seconds
motors.setSpeed(40)
wait(seconds: 2)
// Stop for 1 second
motors.stop(applyBrakes: true)
wait(seconds: 1)
motorA.setSpeed(100)
wait(milliseconds: 100)
motorB.setSpeed(75)
wait(milliseconds: 70)
motorC.setSpeed(50)
wait(milliseconds: 50)
motorD.setSpeed(20)
wait(milliseconds: 80)
// All motors drive backwards in the reverse direction at 40% speed for 2 seconds
motors.setSpeed(-40)
wait(seconds: 3)
// Stop indefinitely
motors.stop(applyBrakes: false)
print("motors test done, yay")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment