2012-02-26 19:13:23 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace ArdupilotMega.Antenna
|
|
|
|
|
{
|
|
|
|
|
class Maestro : ITrackerOutput
|
|
|
|
|
{
|
2012-04-24 10:49:27 -03:00
|
|
|
|
public Comms.SerialPort ComPort { get; set; }
|
2012-02-26 19:13:23 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 0-360
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double TrimPan { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// -90 - 90
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double TrimTilt { get; set; }
|
|
|
|
|
|
|
|
|
|
public int PanStartRange { get; set; }
|
|
|
|
|
public int TiltStartRange { get; set; }
|
|
|
|
|
public int PanEndRange { get; set; }
|
|
|
|
|
public int TiltEndRange { get; set; }
|
2012-03-23 09:52:12 -03:00
|
|
|
|
public int PanPWMRange { get; set; }
|
|
|
|
|
public int TiltPWMRange { get; set; }
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
|
|
|
|
public bool PanReverse { get { return _panreverse == -1; } set { _panreverse = value == true ? -1 : 1 ; } }
|
|
|
|
|
public bool TiltReverse { get { return _tiltreverse == -1; } set { _tiltreverse = value == true ? -1 : 1; } }
|
|
|
|
|
|
|
|
|
|
int _panreverse = 1;
|
|
|
|
|
int _tiltreverse = 1;
|
|
|
|
|
|
|
|
|
|
byte PanAddress = 0;
|
|
|
|
|
byte TiltAddress = 1;
|
|
|
|
|
|
|
|
|
|
public bool Init()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if ((PanStartRange - PanEndRange) == 0)
|
|
|
|
|
{
|
2012-03-09 11:18:12 -04:00
|
|
|
|
System.Windows.Forms.CustomMessageBox.Show("Invalid Pan Range", "Error");
|
2012-02-26 19:13:23 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((TiltStartRange - TiltEndRange) == 0)
|
|
|
|
|
{
|
2012-03-09 11:18:12 -04:00
|
|
|
|
System.Windows.Forms.CustomMessageBox.Show("Invalid Tilt Range", "Error");
|
2012-02-26 19:13:23 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ComPort.Open();
|
|
|
|
|
}
|
2012-03-09 11:18:12 -04:00
|
|
|
|
catch (Exception ex) { System.Windows.Forms.CustomMessageBox.Show("Connect failed " + ex.Message,"Error"); return false; }
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public bool Setup()
|
|
|
|
|
{
|
|
|
|
|
int target = 100;
|
|
|
|
|
// speed
|
|
|
|
|
var buffer = new byte[] { 0x87, PanAddress, (byte)(target & 0x7F), (byte)((target >> 7) & 0x7F) };
|
|
|
|
|
ComPort.Write(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
buffer = new byte[] { 0x87, TiltAddress, (byte)(target & 0x7F), (byte)((target >> 7) & 0x7F) };
|
|
|
|
|
ComPort.Write(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
// accel
|
2012-02-29 09:19:54 -04:00
|
|
|
|
target = 5;
|
2012-02-26 19:13:23 -04:00
|
|
|
|
buffer = new byte[] { 0x89, PanAddress, (byte)(target & 0x7F), (byte)((target >> 7) & 0x7F) };
|
|
|
|
|
ComPort.Write(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
buffer = new byte[] { 0x89, TiltAddress, (byte)(target & 0x7F), (byte)((target >> 7) & 0x7F) };
|
|
|
|
|
ComPort.Write(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double wrap_180(double input)
|
|
|
|
|
{
|
|
|
|
|
if (input > 180)
|
|
|
|
|
return input - 360;
|
|
|
|
|
if (input < -180)
|
|
|
|
|
return input + 360;
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-29 09:19:54 -04:00
|
|
|
|
double wrap_range(double input,double range)
|
|
|
|
|
{
|
|
|
|
|
if (input > range)
|
|
|
|
|
return input - 360;
|
|
|
|
|
if (input < -range)
|
|
|
|
|
return input + 360;
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-26 19:13:23 -04:00
|
|
|
|
public bool Pan(double Angle)
|
2012-03-29 19:17:06 -03:00
|
|
|
|
{
|
2012-02-26 19:13:23 -04:00
|
|
|
|
double range = Math.Abs(PanStartRange - PanEndRange);
|
|
|
|
|
|
2012-02-29 09:19:54 -04:00
|
|
|
|
// get relative center based on tracking center
|
|
|
|
|
double rangeleft = PanStartRange - TrimPan;
|
|
|
|
|
double rangeright = PanEndRange - TrimPan;
|
|
|
|
|
double centerpos = 127;
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
2012-02-29 09:19:54 -04:00
|
|
|
|
// get the output angle the tracker needs to point and constrain the output to the allowed options
|
2012-02-26 19:13:23 -04:00
|
|
|
|
short PointAtAngle = Constrain(wrap_180(Angle - TrimPan), PanStartRange, PanEndRange);
|
|
|
|
|
|
2012-02-29 09:19:54 -04:00
|
|
|
|
// conver the angle into a 0-255 value
|
|
|
|
|
byte target = (byte)((((PointAtAngle / range) * 2.0) * 127 + centerpos) * _panreverse);
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
2012-03-29 19:17:06 -03:00
|
|
|
|
Console.WriteLine("P " + Angle + " " + target + " " + PointAtAngle);
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[] { 0xff,PanAddress,target};
|
|
|
|
|
ComPort.Write(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Tilt(double Angle)
|
|
|
|
|
{
|
|
|
|
|
double range = Math.Abs(TiltStartRange - TiltEndRange);
|
|
|
|
|
|
2012-02-29 09:19:54 -04:00
|
|
|
|
short PointAtAngle = Constrain((Angle - TrimTilt), TiltStartRange, TiltEndRange);
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
|
|
|
|
byte target = (byte)((((PointAtAngle / range ) * 2) * 127 + 127) * _tiltreverse);
|
|
|
|
|
|
2012-03-02 06:40:20 -04:00
|
|
|
|
//Console.WriteLine("T " + Angle + " " + target + " " + PointAtAngle);
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
|
|
|
|
var buffer = new byte[] { 0xff, TiltAddress, target };
|
|
|
|
|
ComPort.Write(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool PanAndTilt(double pan, double tilt)
|
|
|
|
|
{
|
|
|
|
|
if (Tilt(tilt) && Pan(pan))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
2012-03-02 06:40:20 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ComPort.Close();
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
2012-02-26 19:13:23 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
short Constrain(double input, double min, double max)
|
|
|
|
|
{
|
|
|
|
|
if (input < min)
|
|
|
|
|
return (short)min;
|
|
|
|
|
if (input > max)
|
|
|
|
|
return (short)max;
|
|
|
|
|
return (short)input;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|