I’ve got it all up and running switching motors with transistors. I do it through TeraTerm using Pinnacle Game Profiler and an xbox 360 controller to change the controller buttons into keyboard presses that teraterm will understand. However it occassionally crashes the panda and i have to take out the USB and restart it and whatnot, can anyone see why?
Here’s the code
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.Hardware;
using System;
namespace USBClient_Example
{
public class Program
{
public static void Main()
{
// Check debug interface
if (Configuration.DebugInterface.GetCurrent() != Configuration.DebugInterface.Port.USB1)
throw new InvalidOperationException("Interface must be USB.");
// Start CDC
USBC_CDC cdc = USBClientController.StandardDevices.StartCDC_WithDebugging();
byte[] Buffer = new byte[1];
OutputPort LeftThruster = new OutputPort((Cpu.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di6, false);
OutputPort LeftThrusterReverse = new OutputPort((Cpu.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di1, false);
OutputPort RightThruster = new OutputPort((Cpu.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di8, false);
OutputPort RightThrusterReverse = new OutputPort((Cpu.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di11, false);
OutputPort SideRightThruster = new OutputPort((Cpu.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di9, false);
OutputPort SideRightThrusterReverse = new OutputPort((Cpu.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di12, false);
OutputPort SideLeftThruster = new OutputPort((Cpu.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di10, false);
OutputPort SideLeftThrusterReverse = new OutputPort((Cpu.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di13, false);
PWM servo = new PWM((PWM.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.PWM.Di5);
cdc.ReadTimeout = 1000;
while (true)
{
cdc.Read(Buffer, 0, 1);
switch ((char)Buffer[0])
{
case 'Q':
{
LeftThruster.Write(true);
Buffer[0] = 0;
LeftThrusterReverse.Write(false);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Left Thruster Forward\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'W':
{
LeftThruster.Write(false);
Buffer[0] = 0;
LeftThrusterReverse.Write(false);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Left Thruster off\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'E':
{
LeftThruster.Write(true);
Buffer[0] = 0;
LeftThrusterReverse.Write(true);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Left Thruster Reverse\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'R':
{
RightThruster.Write(true);
Buffer[0] = 0;
RightThrusterReverse.Write(false);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Right Thuster Forward\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'T':
{
RightThruster.Write(false);
Buffer[0] = 0;
RightThrusterReverse.Write(false);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Right Thuster Off\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'Y':
{
RightThruster.Write(true);
Buffer[0] = 0;
RightThrusterReverse.Write(true);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Right Thuster Reverse\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'A':
{
SideLeftThruster.Write(true);
Buffer[0] = 0;
SideLeftThrusterReverse.Write(false);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Side Left Thruster Forward/Down\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'S':
{
SideLeftThruster.Write(false);
Buffer[0] = 0;
SideLeftThrusterReverse.Write(false);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Side Left Thruster Off\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'D':
{
SideLeftThruster.Write(true);
Buffer[0] = 0;
SideLeftThrusterReverse.Write(true);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Side Left Thruster Reverse/up\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'F':
{
SideRightThruster.Write(true);
Buffer[0] = 0;
SideRightThrusterReverse.Write(false);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Side Right Thruster Forward/Down\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'G':
{
SideRightThruster.Write(false);
Buffer[0] = 0;
SideRightThrusterReverse.Write(false);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Side Right Thruster off\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'H':
{
SideRightThruster.Write(true);
Buffer[0] = 0;
SideRightThrusterReverse.Write(true);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Side Right Thruster Reverse/Up\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'Z':
{
servo.SetPulse(20 * 1000 * 1000, 1000 * 1000);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("servo 1\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'X':
{
servo.SetPulse(20 * 1000 * 1000, 1250 * 1000);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("servo 1.25\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'C':
{
servo.SetPulse(20 * 1000 * 1000, 1500 * 1000);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("servo 1.5\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'V':
{
servo.SetPulse(20 * 1000 * 1000, 1750 * 1000);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("servo 1.75\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'B':
{
servo.SetPulse(20 * 1000 * 1000, 2000 * 1000);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("servo 2\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
case 'N':
{
servo.SetPulse(20 * 1000 * 1000, 2250 * 1000);
Buffer[0] = 0;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("servo 2.25\r\n");
cdc.Write(bytes, 0, bytes.Length);
}
break;
}
}
}
}
}
Is there anything wrong with this code or should is it just something i’ll have to live with? It seems to crash more often when pressing lots of different buttons at a time