Thanks jdal. I used my old proboat ESC and put together the following code. Works like a charm. I guess the only thing thing I have to work on is being able to build in adjustments to the low/high depending on the esc. They definitly start responding at different ranges.
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
namespace FEZ_Panda_II_Application1
{
public class Program
{
static OutputPort led = null;
static Int32 delay = 1000;
public static void Main()
{
try
{
//set led for flashing when sending pulses
led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
//execute procedures for each test
TestServo();
TestMotorForwardOnly();
TestMotorForwardReverse();
}
catch( Exception ex)
{
Debug.Print("Exception[" + ex.ToString() + "]");
}
finally
{
//dispose
led = null;
}
}
static void TestMotorForwardReverse()
{
PWM forwardreversemotor = null;
try
{
forwardreversemotor = new PWM(PWM.Pin.PWM3);
led.Write(true);
forwardreversemotor.Set(true);
//set esc to neutral ( initialize ESC )
delay = 5000;
SetPulse(forwardreversemotor, 20000, TestMotorForwardReverse_CalcScale(0));
//increase esc from 0 ( neutral ) to 100 ( full forward ) with 1/2 second delay between each pulse
delay = 500;
for (short percent = 0; percent <= 100; percent++)
{
SetPulse(forwardreversemotor, 20000, TestMotorForwardReverse_CalcScale(percent));
}
//set esc to neutral
delay = 5000;
SetPulse(forwardreversemotor, 20000, TestMotorForwardReverse_CalcScale(0));
//increase ecs from 0 ( neutral ) to -100 ( full reverse ) with 1/2 second delay between each pulse
delay = 500;
for (short percent = 0; percent >=-100; percent--)
{
SetPulse(forwardreversemotor, 20000, TestMotorForwardReverse_CalcScale(percent));
}
//set esc to neutral
delay = 5000;
SetPulse(forwardreversemotor, 20000, TestMotorForwardReverse_CalcScale(0));
}
catch (Exception ex)
{
Debug.Print("Exception[" + ex.ToString() + "]");
}
finally
{
//dispose
forwardreversemotor.Set(false);
forwardreversemotor = null;
}
}
static uint TestMotorForwardReverse_CalcScale(short percent)
{
uint fullreverse = 1000;
uint neutral = 1500;
uint fullforward = 2000; //only for reference, not used within calculation
uint perpercent = 0;
uint rtObject = 0;
try
{
//perpercent is the amount to adjust from neutral for each percent
//range is from -100 ( full reverse ) to +100 ( full forward )(
//0 = neutral
perpercent = ( neutral - fullreverse ) / 100;
rtObject = neutral + (uint)(perpercent * percent);
}
catch (Exception ex)
{
Debug.Print("Exception[" + ex.ToString() + "]");
}
finally
{
}
return rtObject;
}
static void TestMotorForwardOnly()
{
PWM forwardmotor = null;
try
{
led.Write(true);
forwardmotor = new PWM(PWM.Pin.PWM2);
forwardmotor.Set(true);
//set esc to 0 ( off )
delay = 5000;
SetPulse(forwardmotor, 20000, TestMotorForwardOnly_CalcScale(0));
//increase esc from 0 ( off ) to 100 ( full ) with 1/2 second delay between each pulse
delay = 500;
for (short percent = 0; percent <= 100; percent++)
{
SetPulse(forwardmotor, 20000, TestMotorForwardOnly_CalcScale(percent));
}
//set esc to 0 ( off )
delay = 5000;
SetPulse(forwardmotor, 20000, TestMotorForwardOnly_CalcScale(0));
}
catch (Exception ex)
{
Debug.Print("Exception[" + ex.ToString() + "]");
}
finally
{
//dispose
forwardmotor.Set(false);
forwardmotor = null;
}
}
static uint TestMotorForwardOnly_CalcScale(short percent)
{
uint low = 1000;
uint high = 2000;
uint rtObject = 0;
uint perpercent = 0;
try
{
//perpercent is the amount to adjust for each percent
//range is from 0 ( off ) to 100 ( full )
perpercent = (high - low) / 100;
rtObject = low + (uint)(perpercent * percent);
}
catch (Exception ex)
{
Debug.Print("Exception[" + ex.ToString() + "]");
}
finally
{
}
return rtObject;
}
static void TestServo()
{
PWM servo = null;
uint low = 600;
uint high = 2400;
uint[] highTime = null;
try
{
led.Write(true);
servo = new PWM(PWM.Pin.PWM1);
servo.Set(true);
//this array is just used to set servo to 0 degrees and flip back and forth between -45 and +45
highTime = new uint[5];
highTime[0] = TestServo_CalcScale(low, high, 0);
highTime[1] = TestServo_CalcScale(low, high, -45);
highTime[2] = TestServo_CalcScale(low, high, 45);
highTime[3] = TestServo_CalcScale(low, high, -45);
highTime[4] = TestServo_CalcScale(low, high, 45);
//iterate through highTime array setting servo location with 2second delay between pulses
delay = 2000;
for( int index = 0; index <= highTime.Length - 1; index++)
{
SetPulse(servo, 20000, highTime[index]);
}
//move servo from -75 to +75 degrees with 1/10 second delay between pulses;
//if servo is capable of -180 to +180 you can use those values.
//I've found a few servos that start freaking out around 170+- so check your servo before breaking it.
delay = 100;
for (short degree = -75; degree <= 75; degree++)
{
SetPulse(servo, 20000, TestServo_CalcScale(low, high, degree));
}
//set servo to 0 degrees
SetPulse(servo, 20000, TestServo_CalcScale(low, high, 0));
}
catch (Exception ex)
{
Debug.Print("Exception[" + ex.ToString() + "]");
}
finally
{
//dispose
servo.Set(false);
servo = null;
}
}
static uint TestServo_CalcScale(uint low, uint high, short degree)
{
uint high_zero = 0;
uint rtObject = 0;
uint perdegree = 0;
try
{
high_zero = low + ( ( high - low ) / 2 );
perdegree = (( high - low ) / 2 ) / 90;
rtObject = high_zero + (uint)( perdegree * degree);
}
catch( Exception ex)
{
Debug.Print("Exception[" + ex.ToString() + "]");
}
finally
{
}
return rtObject;
}
static void SetPulse(PWM pwm, uint period_nanosecond, uint highTime_nanosecond)
{
try
{
Debug.Print("SetPulse: " + period_nanosecond.ToString() + ": " + highTime_nanosecond.ToString());
pwm.SetPulse(period_nanosecond * 1000, highTime_nanosecond * 1000);
//invert LED value ( used to blink led on each pulse )
led.Write(!led.Read());
//delay for specified interval
Thread.Sleep(delay);
}
catch (Exception ex)
{
Debug.Print("Exception[" + ex.ToString() + "]");
}
finally
{
}
}
}
}