Phidget Rover Kit - Acceleration

High Acceleration

Write code (Java)

Not your programming language? Set your preferences so we can display relevant code examples

  
package phidgetsrover;

//Add Phidgets Library
import com.phidget22.*;

public class PhidgetsRover {
    public static void main(String[] args) throws Exception {

        //Connect to wireless rover
        Net.addServer("", "192.168.100.1", 5661, "", 0);

        //Create
        DCMotor leftMotors = new DCMotor();
        DCMotor rightMotors = new DCMotor();

        //Address
        leftMotors.setChannel(0);
        rightMotors.setChannel(1);

        //Open
        leftMotors.open(5000);
        rightMotors.open(5000);

        //Increase acceleration
        leftMotors.setAcceleration(leftMotors.getMaxAcceleration());
        rightMotors.setAcceleration(rightMotors.getMaxAcceleration());

         //Move forward at full speed
        leftMotors.setTargetVelocity(1);
        rightMotors.setTargetVelocity(1);

        //Wait for 1 second
        Thread.sleep(1000);

        //Reverse at full speed
        leftMotors.setTargetVelocity(-1);
        rightMotors.setTargetVelocity(-1);

        //Wait for 1 second
        Thread.sleep(1000);

        //Stop motors
        leftMotors.setTargetVelocity(0);
        rightMotors.setTargetVelocity(0);
    }
}

  
//Add Phidgets Library
import com.phidget22.*;

public class PhidgetsRover {
    public static void main(String[] args) throws Exception {

        //Connect to wireless rover
        Net.addServer("", "192.168.100.1", 5661, "", 0);

        //Create
        DCMotor leftMotors = new DCMotor();
        DCMotor rightMotors = new DCMotor();

        //Address
        leftMotors.setHubPort(5);
        leftMotors.setChannel(0);
        rightMotors.setHubPort(5);
        rightMotors.setChannel(1);

        //Open
        leftMotors.open(5000);
        rightMotors.open(5000);

        //Increase acceleration
        leftMotors.setAcceleration(leftMotors.getMaxAcceleration());
        rightMotors.setAcceleration(rightMotors.getMaxAcceleration());

         //Move forward at full speed
        leftMotors.setTargetVelocity(1);
        rightMotors.setTargetVelocity(1);

        //Wait for 1 second
        Thread.sleep(1000);

        //Reverse at full speed
        leftMotors.setTargetVelocity(-1);
        rightMotors.setTargetVelocity(-1);

        //Wait for 1 second
        Thread.sleep(1000);

        //Stop motors
        leftMotors.setTargetVelocity(0);
        rightMotors.setTargetVelocity(0);
    }
}

  
//Add Phidgets Library 
import com.phidget22.*;

//Define
DCMotor leftMotors;
DCMotor rightMotors;

void setup(){
  try{
    
    //Connect to wireless rover
    Net.addServer("","192.168.100.1", 5661,"",0);
    
    //Create
    leftMotors = new DCMotor();
    rightMotors = new DCMotor();
    
    //Address 
    leftMotors.setChannel(0);
    rightMotors.setChannel(1);
    
    //Open
    leftMotors.open(5000);
    rightMotors.open(5000);
    
    //Increase acceleration
    leftMotors.setAcceleration(leftMotors.getMaxAcceleration());
    rightMotors.setAcceleration(rightMotors.getMaxAcceleration());
    
  }catch(Exception e){
    //Handle Exceptions
    e.printStackTrace();
  }
}

void draw(){
  try{
    
    //Move forward at full speed
    leftMotors.setTargetVelocity(1);
    rightMotors.setTargetVelocity(1);
    
    //Wait for 1 second
    delay(1000);
    
    //Reverse at full speed
    leftMotors.setTargetVelocity(-1);
    rightMotors.setTargetVelocity(-1);
    
    //Wait for 1 second
    delay(1000);
    
    //Stop motors
    leftMotors.setTargetVelocity(0);
    rightMotors.setTargetVelocity(0);
    
    //Only execute draw once for this example
    noLoop();
    
  }catch(Exception e){
    //Handle Exceptions
    e.printStackTrace();
  }
}

Write code (Python)

Not your programming language? Set your preferences so we can display relevant code examples.

  

#Add Phidgets library
from Phidget22.Phidget import *
from Phidget22.Net import *
from Phidget22.Devices.DCMotor import *
#Required for sleep statement
import time

#Connect to your rover
Net.addServer("", "192.168.100.1", 5661, "", 0)

#Create
leftMotors = DCMotor()
rightMotors = DCMotor()

#Address
leftMotors.setChannel(0)
rightMotors.setChannel(1)

#Open
leftMotors.openWaitForAttachment(5000)
rightMotors.openWaitForAttachment(5000)

#Increase acceleration
leftMotors.setAcceleration(leftMotors.getMaxAcceleration())
rightMotors.setAcceleration(rightMotors.getMaxAcceleration())

#Move forward at full speed
leftMotors.setTargetVelocity(1)
rightMotors.setTargetVelocity(1)

#Wait for 1 second
time.sleep(1)

#Reverse at full speed
leftMotors.setTargetVelocity(-1)
rightMotors.setTargetVelocity(-1)

#Wait for 1 second
time.sleep(1)

#Stop motors
leftMotors.setTargetVelocity(0)
rightMotors.setTargetVelocity(0)
  

Write code (C#)

Not your programming language? Set your preferences so we can display relevant code examples

  

//Add Phidgets Library
using Phidget22;

namespace PhidgetsRover{
    class Program{
        static void Main(string[] args){

            //Connect to wireless rover
            Net.AddServer("", "192.168.100.1", 5661, "", 0);

            //Create
            DCMotor leftMotors = new DCMotor();
            DCMotor rightMotors = new DCMotor();

            //Address
            leftMotors.Channel = 0;
            rightMotors.Channel = 1;

            //Open
            leftMotors.Open(5000);
            rightMotors.Open(5000);

            //Increase acceleration
            leftMotors.Acceleration = leftMotors.MaxAcceleration;
            rightMotors.Acceleration = rightMotors.MaxAcceleration;

            //Move forward at full speed
            leftMotors.TargetVelocity = 1;
            rightMotors.TargetVelocity = 1;

            //Wait for 1 second
            System.Threading.Thread.Sleep(1000);

            //Reverse at full speed
            leftMotors.TargetVelocity = -1;
            rightMotors.TargetVelocity = -1;

            //Wait for 1 second
            System.Threading.Thread.Sleep(1000);

            //Stop motors
            leftMotors.TargetVelocity = 0;
            rightMotors.TargetVelocity = 0;
        }
    }
}
  

Write code (Swift)

Not your programming language? Set your preferences so we can display relevant code examples

Create two buttons

  

import Cocoa
//Add Phidgets Library
import Phidget22Swift

class ViewController: NSViewController {
    
    //Create
    let leftMotors = DCMotor()
    let rightMotors = DCMotor()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        do{
            //Connect to wireless rover
            try Net.addServer(serverName: "", address: "192.168.100.1", port: 5661, flags: 0)
            
            //Address
            try leftMotors.setChannel(0)
            try rightMotors.setChannel(1)
            
            //Subscribe to event
            let _ = leftMotors.attach.addHandler(leftMotors_onAttach)
            let _ = rightMotors.attach.addHandler(rightMotors_onAttach)
            
            //Open
            try leftMotors.open()
            try rightMotors.open()
            
        }catch{
            print(error)
        }
    }
    
    func leftMotors_onAttach(sender:Phidget){
        do{
            //Increase acceleration
            try leftMotors.setAcceleration(leftMotors.getMaxAcceleration())
        }catch{
            print(error)
        }
    }
    
    func rightMotors_onAttach(sender:Phidget){
        do{
            //Increase acceleration
            try rightMotors.setAcceleration(rightMotors.getMaxAcceleration())
        }catch{
            print(error)
        }
    }
    
    @IBAction func moveRover(_ sender: Any) {
        do{
            
            //Move forward at full speed
            try leftMotors.setTargetVelocity(1)
            try rightMotors.setTargetVelocity(1)
            
            //Wait for 1 second
            sleep(1)
            
            //Reverse at full speed
            try leftMotors.setTargetVelocity(-1)
            try rightMotors.setTargetVelocity(-1)
            
            //Wait for 1 second
            sleep(1)
            
            //Stop motors
            try leftMotors.setTargetVelocity(0)
            try rightMotors.setTargetVelocity(0)
            
        }catch{
            print(error)
        }
    }
}
  

Code review

After running the code above, you will notice that the rover now performs as you would expect, and ends up in approximately the same position it started. Moving forward, make sure to always increase the acceleration in your rover projects!

For advanced users: If you have not completed the Getting Started Kit Advanced Lessons about Using the Sensor API and Using the Phidget API, consider reviewing them. The 2x DC Motor Phidget does not have a device tutorial page, however, you can view the API on the product page here.

What’s Next

Congratulations on improving your rover! Improve it further by pressing the Finish button below and moving onto the Thumbstick project!

Troubleshoot
  1. Your rover should drive in the direction that the Sonar Phidget is facing. If it isn’t, simply use negative values for your Target Velocity. This will make the motors spin the opposite direction!
  2. Make sure everything is plugged in properly and your VINT Hub is connected to your computer.
  3. Stop running other programs that are using your Phidgets before running a program.

If you are still having issues visit Advanced Troubleshooting

What are Phidgets?

Phidgets are programmable USB sensors. Simply plug in your sensor, write code in your favorite language and go!

Phidgets have been used by STEM professionals for over 20 years and are now available to students.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done