Phidget Rover Kit - Thumbstick

Move

You can add the following code to your PhidgetsRover project, or create a new project. If you’ve forgotten how to do this, revisit the Configure section from the Getting Started Kit.

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();
        VoltageRatioInput vAxis = new VoltageRatioInput(); 

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

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

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

        //Use your Phidgets
        while(true)
        {
            //Get data from vertical axis (value between -1 and 1)
            double verticalAxis = vAxis.getVoltageRatio();
            
            //Use Thumbstick position set motor controller target velocity
            leftMotors.setTargetVelocity(verticalAxis);
            rightMotors.setTargetVelocity(verticalAxis);

            //Wait 100 milliseconds
            Thread.sleep(100);
        }
    }
}

  
//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();
        VoltageRatioInput vAxis = new VoltageRatioInput(); 

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

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

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

        //Use your Phidgets
        while(true)
        {
            //Get data from vertical axis (value between -1 and 1)
            double verticalAxis = vAxis.getVoltageRatio();
            
            //Use Thumbstick position set motor controller target velocity
            leftMotors.setTargetVelocity(verticalAxis);
            rightMotors.setTargetVelocity(verticalAxis);
            
           //Wait 100 milliseconds
            Thread.sleep(100);
        }
    }
}

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

//Define
DCMotor leftMotors;
DCMotor rightMotors;
VoltageRatioInput vAxis;

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

void draw(){
  try{
    
    //Get data from vertical axis (value between -1 and 1)
    double verticalAxis = vAxis.getVoltageRatio();
    
    //Use Thumbstick position to set motor controller target velocity
    leftMotors.setTargetVelocity(verticalAxis);
    rightMotors.setTargetVelocity(verticalAxis);
    
    //Wait 100 milliseconds
    delay(100);
    
  }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 *
from Phidget22.Devices.VoltageRatioInput import *
#Required for sleep statement
import time

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

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

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

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

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

#Use your Phidgets
while(True):
    #Get data from vertical axis (value between -1 and 1)
    verticalAxis = vAxis.getVoltageRatio()

    #Use Thumbstick position set motor controller target velocity
    leftMotors.setTargetVelocity(verticalAxis)
    rightMotors.setTargetVelocity(verticalAxis)
    
    #Wait 100 milliseconds
    time.sleep(0.1)

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();
            VoltageRatioInput vAxis = new VoltageRatioInput();

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

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

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

            //Use your Phidgets
            while(true) 
            {
                //Get data from vertical axis (value between -1 and 1)
                double verticalAxis = vAxis.VoltageRatio;

                //Use Thumbstick position set motor controller target velocity
                leftMotors.TargetVelocity = verticalAxis;
                rightMotors.TargetVelocity = verticalAxis;
                
                //Wait for 100 milliseconds
                System.Threading.Thread.Sleep(100);
            }
        }
    }
}

Write code (Swift)

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

Create two buttons

  
    import Cocoa
    import Phidget22Swift

    @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {
      @IBOutlet weak var window: NSWindow!
      // Create
      let leftMotors = DCMotor()
      let rightMotors = DCMotor()
      let vAxis = VoltageRatioInput()

      @IBAction func moveRover(_ sender: Any) {
      // Not used in this section
      }

      // Set left acceleration
      func onLeftMotorsAttached(sender: Phidget) {
        do {
          try leftMotors.setAcceleration(leftMotors.getMaxAcceleration())
        }catch {
          print(error)
        }
      }

      // Set Right acceleration
      func onRightMotorsAttached(sender: Phidget) {
        do {
          try rightMotors.setAcceleration(rightMotors.getMaxAcceleration())
        } catch {
          print(error)
        }
      }

      // Move Rover with Thumbstick
      func onVerticalAxisChange(sender: VoltageRatioInput, voltageRatio: Double) {
        do {
          try leftMotors.setTargetVelocity(voltageRatio)
          try rightMotors.setTargetVelocity(voltageRatio)
        } catch {
          print(error)
        }
      }

      func applicationDidFinishLaunching(_ aNotification: Notification) {
        do {
          // Connect to wireless rover
          try Net.addServer(serverName: "", address: "192.168.100.1", port: 5661, flags: 0)

          // Address
          try leftMotors.setHubPort(5)
          try leftMotors.setChannel(0)
          let _ = leftMotors.attach.addHandler(onLeftMotorsAttached)

          try rightMotors.setHubPort(5)
          try rightMotors.setChannel(1)
          let _ = rightMotors.attach.addHandler(onRightMotorsAttached)

          try vAxis.setHubPort(0)
          try vAxis.setChannel(0)
          let _ = vAxis.voltageRatioChange.addHandler(onVerticalAxisChange)

          // Open
          try leftMotors.open()
          try rightMotors.open() try vAxis.open()
        } catch {
          print(error)
        }
      }
    }
  

Code review

The Thumbstick Phidget records position in two axes – horizontal and vertical. The position in each axis is represented by a value between 1 and -1, we can directly use these values to set the velocity. Setting the channel distinguishes the two axes in the program

Practice

  1. Try printing the value of the vertical axis to the screen to understand how the thumbstick works. What is the output range from the thumbstick?
  2. Try modifying your code so that it uses the horizontal axis instead of the vertical axis. (Hint: channel should be 1 instead of 0).

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