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();
VoltageRatioInput hAxis = new VoltageRatioInput();
//Address
leftMotors.setChannel(0);
rightMotors.setChannel(1);
vAxis.setChannel(0);
hAxis.setChannel(1);
//Open
leftMotors.open(5000);
rightMotors.open(5000);
vAxis.open(5000);
hAxis.open(5000);
//Increase acceleration
leftMotors.setAcceleration(leftMotors.getMaxAcceleration());
rightMotors.setAcceleration(rightMotors.getMaxAcceleration());
//Use your Phidgets
while(true){
//Get data from vertical and horizontal axis (values between -1 and 1)
double verticalAxis = vAxis.getVoltageRatio();
double horizontalAxis = hAxis.getVoltageRatio();
//Use thumbstick data to figure how each side of rover should move
double leftMotorsSpeed = verticalAxis + horizontalAxis;
double rightMotorsSpeed = verticalAxis - horizontalAxis;
//Limit values to between -1 and 1
if(leftMotorsSpeed > 1) leftMotorsSpeed = 1;
if(leftMotorsSpeed < -1) leftMotorsSpeed = -1;
if(rightMotorsSpeed > 1) rightMotorsSpeed = 1;
if(rightMotorsSpeed < -1) rightMotorsSpeed = -1;
//Apply values
leftMotors.setTargetVelocity(leftMotorsSpeed);
rightMotors.setTargetVelocity(rightMotorsSpeed);
//Wait for 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();
VoltageRatioInput hAxis = new VoltageRatioInput();
//Address
leftMotors.setChannel(0);
rightMotors.setChannel(1);
vAxis.setChannel(0);
hAxis.setChannel(1);
//Open
leftMotors.open(5000);
rightMotors.open(5000);
vAxis.open(5000);
hAxis.open(5000);
//Increase acceleration
leftMotors.setAcceleration(leftMotors.getMaxAcceleration());
rightMotors.setAcceleration(rightMotors.getMaxAcceleration());
//Use your Phidgets
while(true){
//Get data from vertical and horizontal axis (values between -1 and 1)
double verticalAxis = vAxis.getVoltageRatio();
double horizontalAxis = hAxis.getVoltageRatio();
//Use thumbstick data to figure how each side of rover should move
double leftMotorsSpeed = verticalAxis + horizontalAxis;
double rightMotorsSpeed = verticalAxis - horizontalAxis;
//Limit values to between -1 and 1
if(leftMotorsSpeed > 1) leftMotorsSpeed = 1;
if(leftMotorsSpeed < -1) leftMotorsSpeed = -1;
if(rightMotorsSpeed > 1) rightMotorsSpeed = 1;
if(rightMotorsSpeed < -1) rightMotorsSpeed = -1;
//Apply values
leftMotors.setTargetVelocity(leftMotorsSpeed);
rightMotors.setTargetVelocity(rightMotorsSpeed);
//Wait for 100 milliseconds
Thread.sleep(100);
}
}
}
//Add Phidgets Library
import com.phidget22.*;
//Define
DCMotor leftMotors;
DCMotor rightMotors;
VoltageRatioInput vAxis;
VoltageRatioInput hAxis;
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();
hAxis = new VoltageRatioInput();
//Address
leftMotors.setChannel(0);
rightMotors.setChannel(1);
vAxis.setChannel(0);
hAxis.setChannel(1);
//Open
leftMotors.open(5000);
rightMotors.open(5000);
vAxis.open(5000);
hAxis.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 and horiztonal axis (value between -1 and 1)
double verticalAxis = vAxis.getVoltageRatio();
double horizontalAxis = hAxis.getVoltageRatio();
//Use thumbstick data to figure out how each side of rover should move
double leftMotorSpeed = verticalAxis + horizontalAxis;
double rightMotorSpeed = verticalAxis - horizontalAxis;
//Limit values to between -1 and 1
if(leftMotorSpeed > 1) leftMotorSpeed = 1;
if(leftMotorSpeed < -1) leftMotorSpeed = -1;
if(rightMotorSpeed > 1) rightMotorSpeed = 1;
if(rightMotorSpeed < -1) rightMotorSpeed = -1;
//Use Thumbstick position to set motor controller target velocity
leftMotors.setTargetVelocity(leftMotorSpeed);
rightMotors.setTargetVelocity(rightMotorSpeed);
//Wait for 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()
hAxis = VoltageRatioInput()
#Address
leftMotors.setChannel(0)
rightMotors.setChannel(1)
vAxis.setChannel(0)
hAxis.setChannel(1)
#Open
leftMotors.openWaitForAttachment(5000)
rightMotors.openWaitForAttachment(5000)
vAxis.openWaitForAttachment(5000)
hAxis.openWaitForAttachment(5000)
#Increase acceleration
leftMotors.setAcceleration(leftMotors.getMaxAcceleration())
rightMotors.setAcceleration(rightMotors.getMaxAcceleration())
while(True):
#Get data from vertical and horizontal axis (values between -1 and 1)
verticalAxis = vAxis.getVoltageRatio()
horizontalAxis = hAxis.getVoltageRatio()
#Use thumbstick data to figure how each side of rover should move
leftMotorsSpeed = verticalAxis + horizontalAxis
rightMotorsSpeed = verticalAxis - horizontalAxis
#Limit values to between -1 and 1
if(leftMotorsSpeed > 1): leftMotorsSpeed = 1
if(leftMotorsSpeed < -1): leftMotorsSpeed = -1
if(rightMotorsSpeed > 1): rightMotorsSpeed = 1
if(rightMotorsSpeed < -1): rightMotorsSpeed = -1
#Apply values
leftMotors.setTargetVelocity(leftMotorsSpeed)
rightMotors.setTargetVelocity(rightMotorsSpeed)
#Wait for 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();
VoltageRatioInput hAxis = new VoltageRatioInput();
//Address
leftMotors.Channel = 0;
rightMotors.Channel = 1;
vAxis.Channel = 0;
hAxis.Channel = 1;
//Open
leftMotors.Open(5000);
rightMotors.Open(5000);
vAxis.Open(5000);
hAxis.Open(5000);
//Increase acceleration
leftMotors.Acceleration = leftMotors.MaxAcceleration;
rightMotors.Acceleration = rightMotors.MaxAcceleration;
//Use your Phidgets
while(true)
{
//Get data from vertical and horizontal axis (values between -1 and 1)
double verticalAxis = vAxis.VoltageRatio;
double horizontalAxis = hAxis.VoltageRatio;
//Use thumbstick data to figure how each side of rover should move
double leftMotorsSpeed = verticalAxis + horizontalAxis;
double rightMotorsSpeed = verticalAxis - horizontalAxis;
//Limit values to between -1 and 1
if (leftMotorsSpeed > 1) leftMotorsSpeed = 1;
if (leftMotorsSpeed < -1) leftMotorsSpeed = -1;
if (rightMotorsSpeed > 1) rightMotorsSpeed = 1;
if (rightMotorsSpeed < -1) rightMotorsSpeed = -1;
//Apply values
leftMotors.TargetVelocity = leftMotorsSpeed;
rightMotors.TargetVelocity = rightMotorsSpeed;
//Wait for 100 milliseconds
System.Threading.Thread.Sleep(100);
}
}
}
}
Write code (Swift)
Create two buttons in your window and copy the code below into your project.
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!
// Variable to store info
var horizontalAxisValue = 0.0
// Create
let leftMotors = DCMotor()
let rightMotors = DCMotor()
let vAxis = VoltageRatioInput()
let hAxis = VoltageRatioInput()
@IBAction func moveRover(_ sender: Any) {
// Not used in this section
}
func onVerticalAxisChange(sender: VoltageRatioInput, voltageRatio: Double) {
let verticalAxisValue = voltageRatio
// Turning code
var leftMotorsSpeed = verticalAxisValue + horizontalAxisValue
var rightMotorsSpeed = verticalAxisValue - horizontalAxisValue
// Make sure input to setTargetVelocity is between -1 and 1
if(leftMotorsSpeed > 1) {
leftMotorsSpeed = 1
}
if(leftMotorsSpeed < -1) {
leftMotorsSpeed = -1
}
if(rightMotorsSpeed > 1) {
rightMotorsSpeed = 1
}
if(rightMotorsSpeed < -1) {
rightMotorsSpeed = -1
}
do {
try leftMotors.setTargetVelocity(leftMotorsSpeed)
try rightMotors.setTargetVelocity(rightMotorsSpeed)
} catch {
print(error)
}
}
func onHorizontalAxisChange(sender: VoltageRatioInput, voltageRatio: Double) {
// Store for use in onVerticalAxisChange
horizontalAxisValue = voltageRatio
}
func onLeftMotorsAttached(sender:Phidget) {
do {
try leftMotors.setAcceleration(leftMotors.getMaxAcceleration())
} catch {
print(error)
}
}
func onRightMotorsAttached(sender:Phidget) {
do {
try rightMotors.setAcceleration(leftMotors.getMaxAcceleration())
} 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)
try hAxis.setHubPort(0)
try hAxis.setChannel(1)
let _ = hAxis.voltageRatioChange.addHandler(onHorizontalAxisChange)
// Open
try leftMotors.open()
try rightMotors.open()
try vAxis.open()
try hAxis.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. Combining the horizontal and vertical data from the thumbstick we can turn the Rover. This is shown in the code above.
Practice
- Try understanding how the turning code works by completing the following table.
double leftMotorsSpeed = verticalAxis + horizontalAxis;
double rightMotorsSpeed = verticalAxis - horizontalAxis;
leftMotorsSpeed = verticalAxis + horizontalAxis
rightMotorsSpeed = verticalAxis - horizontalAxis
double leftMotorsSpeed = verticalAxis + horizontalAxis;
double rightMotorsSpeed = verticalAxis - horizontalAxis;
var leftMotorsSpeed = verticalAxisValue + horizontalAxisValue
var rightMotorsSpeed = verticalAxisValue - horizontalAxisValue
verticalAxis | horizontalAxis | leftMotorsSpeed | rightMotorsSpeed | Result |
---|---|---|---|---|
1.0 | 0.0 | 1.0 | 1.0 | Move Forward |
-1.0 | 0.0 | ? | ? | ? |
0.0 | 1.0 | ? | ? | ? |
0.0 | -1.0 | ? | ? | ? |
2. Add obstacle avoidance to this code. (Hint: this was covered in the Avoiding Obstacles section)
3. The Thumbstick Phidget has a button if you push down on it. Try adding special functionality to your program using the button. For example, if the button is pushed, spin 180°. (Hint: the button functions in the same way as the buttons from the Getting Started Kit, except you do not have to set the isHubPortDevice property to true).