2012-02-26 19:13:23 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2012-11-25 01:42:46 -04:00
|
|
|
|
using System.Windows.Forms;
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
|
|
|
|
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-11-25 01:42:46 -04:00
|
|
|
|
public int PanPWMCenter { get; set; }
|
|
|
|
|
public int TiltPWMCenter { get; set; }
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
2012-06-04 06:56:46 -03:00
|
|
|
|
public bool PanReverse { get { return _panreverse == -1; } set { _panreverse = value == true ? -1 : 1; } }
|
2012-02-26 19:13:23 -04:00
|
|
|
|
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;
|
|
|
|
|
|
2012-11-25 01:42:46 -04:00
|
|
|
|
private const byte SetTarget = 0x84;
|
|
|
|
|
private const byte SetSpeed = 0x87;
|
|
|
|
|
private const byte SetAccel = 0x89;
|
|
|
|
|
private const byte GetPos = 0x90;
|
|
|
|
|
private const byte GetState = 0x93;
|
|
|
|
|
private const byte GetErrors = 0xA1;
|
|
|
|
|
private const byte GoHome = 0xA2;
|
|
|
|
|
|
2012-02-26 19:13:23 -04:00
|
|
|
|
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-06-04 06:56:46 -03: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;
|
|
|
|
|
}
|
2012-11-25 01:42:46 -04:00
|
|
|
|
|
2012-06-04 06:56:46 -03:00
|
|
|
|
public bool Setup()
|
2012-02-26 19:13:23 -04:00
|
|
|
|
{
|
|
|
|
|
int target = 100;
|
|
|
|
|
// speed
|
2012-11-25 01:42:46 -04:00
|
|
|
|
SendCompactMaestroCommand(SetSpeed, 0, PanAddress, target);
|
|
|
|
|
SendCompactMaestroCommand(SetSpeed, 0, TiltAddress, target);
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
|
|
|
|
// accel
|
2012-02-29 09:19:54 -04:00
|
|
|
|
target = 5;
|
2012-11-25 01:42:46 -04:00
|
|
|
|
SendCompactMaestroCommand(SetAccel, 0, PanAddress, target);
|
|
|
|
|
SendCompactMaestroCommand(SetAccel, 0, TiltAddress, target);
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
2012-11-25 01:42:46 -04:00
|
|
|
|
//getCenterPWs();
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-25 01:42:46 -04:00
|
|
|
|
void getCenterPWs()
|
2012-02-26 19:13:23 -04:00
|
|
|
|
{
|
2012-11-25 01:42:46 -04:00
|
|
|
|
byte[] buffer;
|
|
|
|
|
// set all to home (center)
|
|
|
|
|
SendCompactMaestroCommand(GoHome);
|
|
|
|
|
|
|
|
|
|
while (SendCompactMaestroCommand(GetState, 1)[0] == 0x01) { }
|
|
|
|
|
|
|
|
|
|
// get center position -- pan
|
|
|
|
|
buffer = SendCompactMaestroCommand(GetPos, 2, PanAddress);
|
|
|
|
|
this.PanPWMCenter = (int)((buffer[1] >> 8) | buffer[0]);
|
|
|
|
|
|
|
|
|
|
// get center position -- tilt
|
|
|
|
|
buffer = SendCompactMaestroCommand(GetPos, 2, TiltAddress);
|
|
|
|
|
this.TiltPWMCenter = (int)((buffer[1] >> 8) | buffer[0]);
|
2012-02-26 19:13:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-25 01:42:46 -04:00
|
|
|
|
double wrap_180(double input)
|
2012-02-29 09:19:54 -04:00
|
|
|
|
{
|
2012-11-25 01:42:46 -04:00
|
|
|
|
if (input > 180)
|
2012-02-29 09:19:54 -04:00
|
|
|
|
return input - 360;
|
2012-11-25 01:42:46 -04:00
|
|
|
|
if (input < -180)
|
2012-02-29 09:19:54 -04:00
|
|
|
|
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-11-25 01:42:46 -04:00
|
|
|
|
double angleRange = Math.Abs(this.PanStartRange - this.PanEndRange);
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
2012-11-25 01:42:46 -04:00
|
|
|
|
double pulseWidth = ((((double)this.PanPWMRange) / angleRange) * wrap_180(Angle- TrimPan) * _panreverse) + (double)this.PanPWMCenter;
|
|
|
|
|
|
|
|
|
|
short target = Constrain(pulseWidth, this.PanPWMCenter - (this.PanPWMRange / 2), this.PanPWMCenter + (this.PanPWMRange / 2));
|
|
|
|
|
target *= 4;
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
2012-11-25 01:42:46 -04:00
|
|
|
|
SendCompactMaestroCommand(SetTarget, 0, PanAddress, target);
|
2012-06-04 06:56:46 -03:00
|
|
|
|
return true;
|
2012-02-26 19:13:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Tilt(double Angle)
|
|
|
|
|
{
|
2012-11-25 01:42:46 -04:00
|
|
|
|
double angleRange = Math.Abs(this.TiltStartRange - this.TiltEndRange);
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
2012-11-25 01:42:46 -04:00
|
|
|
|
double pulseWidth = ((((double)this.TiltPWMRange) / angleRange) * (Angle - TrimTilt) * _tiltreverse) + (double)this.TiltPWMCenter;
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
2012-11-25 01:42:46 -04:00
|
|
|
|
short target = Constrain(pulseWidth, this.TiltPWMCenter - (this.TiltPWMRange / 2), this.TiltPWMCenter + (this.TiltPWMRange / 2));
|
|
|
|
|
target *= 4;
|
2012-02-26 19:13:23 -04:00
|
|
|
|
|
2012-11-25 01:42:46 -04:00
|
|
|
|
SendCompactMaestroCommand(SetTarget, 0, TiltAddress, target);
|
2012-06-04 06:56:46 -03:00
|
|
|
|
return true;
|
2012-02-26 19:13:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2012-11-25 01:42:46 -04:00
|
|
|
|
|
|
|
|
|
byte[] SendCompactMaestroCommand(byte cmd, int respByteCount = 0, byte addr = 0xFF, int data = -1)
|
|
|
|
|
{
|
|
|
|
|
byte[] buffer;
|
|
|
|
|
if (addr == 0xFF)
|
|
|
|
|
buffer = new byte[] { cmd };
|
|
|
|
|
else if (data < 0)
|
|
|
|
|
buffer = new byte[] { cmd, addr };
|
|
|
|
|
else
|
|
|
|
|
buffer = new byte[] { cmd, addr, (byte)(data & 0x7F), (byte)((data >> 7) & 0x7F) };
|
|
|
|
|
ComPort.DiscardInBuffer();
|
|
|
|
|
ComPort.Write(buffer, 0, buffer.Length);
|
|
|
|
|
if (respByteCount > 0)
|
|
|
|
|
{
|
|
|
|
|
buffer = new byte[respByteCount];
|
|
|
|
|
while (ComPort.BytesToRead < respByteCount) { }
|
|
|
|
|
ComPort.Read(buffer, 0, respByteCount);
|
|
|
|
|
}
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
2012-02-26 19:13:23 -04:00
|
|
|
|
}
|
|
|
|
|
}
|