2025-02-24 22:23:52 +03:00

260 lines
8.6 KiB
C

/*
* This file is part of the SSII project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <math.h>
#include <stdint.h>
#include "sidservo.h"
/*********** base commands ***********/
// get/set X/Y in motsteps
#define CMD_MOTX "X"
#define CMD_MOTY "Y"
// set X/Y position with speed "sprintf(buf, "%s%d%s%d", CMD_MOTx, tagx, CMD_MOTxS, tags)
#define CMD_MOTXYS "S"
// reset current motor position to given value (and stop, if moving)
#define CMD_MOTXSET "XF"
#define CMD_MOTYSET "YF"
// acceleration (per each loop, max: 3900)
#define CMD_MOTXACCEL "XR"
#define CMD_MOTYACCEL "YR"
// PID regulator:
// P: 0..32767
#define CMD_PIDPX "XP"
#define CMD_PIDPY "YP"
// I: 0..32767
#define CMD_PIDIX "XI"
#define CMD_PIDIY "YI"
// limit of I (doesn't work): 0:24000 (WTF???)
#define CMD_PIDILX "XL"
#define CMD_PIDILY "YL"
// D: 0..32767
#define CMD_PIDDX "XD"
#define CMD_PIDDY "YD"
// current position error
#define CMD_POSERRX "XE"
#define CMD_POSERRY "YE"
// max position error limit (X: E#, Y: e#)
#define CMD_POSERRLIMX "XEL"
#define CMD_POSERRLIMY "YEL"
// current PWM output: 0..255 (or set max PWM out)
#define CMD_PWMOUTX "XO"
#define CMD_PWMOUTY "YO"
// motor current *100 (or set current limit): 0..240
#define CMD_MOTCURNTX "XC"
#define CMD_MOTCURNTY "YC"
// change axis to Manual mode and set the PWM output: -255:255
#define CMD_MANUALPWMX "XM"
#define CMD_MANUALPWMY "YM"
// change axis to Auto mode
#define CMD_AUTOX "XA"
#define CMD_AUTOY "YA"
// get positioin in encoders' ticks or reset it to given value
#define CMD_ENCX "XZ"
#define CMD_ENCY "YZ"
// get/set speed (geter x: S#, getter y: s#)
#define CMD_SPEEDX "XS"
#define CMD_SPEEDY "YS"
// normal stop X/Y
#define CMD_STOPX "XN"
#define CMD_STOPY "YN"
// lower speed -> drag&track or slew&track
#define CMD_STOPTRACKX "XNT"
#define CMD_STOPTRACKY "YNT"
// emergency stop
#define CMD_EMSTOPX "XG"
#define CMD_EMSTOPY "YG"
// get/set X/Ybits
#define CMD_BITSX "XB"
#define CMD_BITSY "YB"
/*********** getters/setters without "Y" variant ***********/
// get handpad status (decimal)
#define CMD_HANDPAD "XK"
// get TCPU (deg F)
#define CMD_TCPU "XH"
// get firmware version *10
#define CMD_FIRMVER "XV"
// get motor voltage *10
#define CMD_MOTVOLTAGE "XJ"
// get/set current CPU clock (milliseconds)
#define CMD_MILLIS "XY"
// reset servo
#define CMD_RESET "XQ"
// clear to factory defaults
#define CMD_CLRDEFAULTS "XU"
// save configuration to flash ROM
#define CMD_WRITEFLASH "XW"
// read config from flash to RAM
#define CMD_READFLASH "XT"
// write to flash following full config (128 bytes + 2 bytes of checksum)
#define CMD_PROGFLASH "FC"
// read configuration (-//-)
#define CMD_DUMPFLASH "SC"
// get serial number
#define CMD_SERIAL "YV"
/*********** extended commands ***********/
// get/set latitute
#define CMD_LATITUDE "XXL"
// getters/setters of motor's encoders per rev
#define CMD_MEPRX "XXU"
#define CMD_MEPRY "XXV"
// -//- axis encoders
#define CMD_AEPRX "XXT"
#define CMD_AEPRY "XXZ"
// get/set slew rate
#define CMD_SLEWRATEX "XXA"
#define CMD_SLEWRATEY "XXB"
// get/set pan rate
#define CMD_PANRATEX "XXC"
#define CMD_PANRATEY "XXD"
// get/set platform tracking rate
#define CMD_PLATRATE "XXE"
// get/set platform up/down adjuster
#define CMD_PLATADJ "XXF"
// get/set platform goal
#define CMD_PLATGOAL "XXG"
// get/set guide rate
#define CMD_GUIDERATEX "XXH"
#define CMD_GUIDERATEY "XXI"
// get/set picservo timeout (seconds)
#define CMD_PICTMOUT "XXJ"
// get/set digital outputs of radio handpad
#define CMD_RADIODIGOUT "XXQ"
// get/set argo navis mode
#define CMD_ARGONAVIS "XXN"
// get/set local search distance
#define CMD_LOSCRCHDISTX "XXM"
#define CMD_LOSCRCHDISTY "XXO"
// get/set backlash
#define CMD_BACKLASHX "XXO"
#define CMD_BACKLASHY "XXP"
// get binary data of all statistics
#define CMD_GETSTAT "XXS"
// send short command
#define CMD_SHORTCMD "XXR"
// send long command
#define CMD_LONGCMD "YXR"
/*********** special ***********/
// exit ASCII checksum mode
#define CMD_EXITACM "YXY0\r\xb8"
// controller status:
// X# Y# XZ# YZ# XC# YC# V# T# X[AM] Y[AM] K#
// X,Y - motor, XZ,YZ - encoder, XC,YC - current*100, V - voltage*10, T - temp (F), XA,YA - mode (A[uto]/M[anual]), K - handpad status bits
#define CMD_GETSTATTEXT "\r"
// steps per revolution
//#define X_MOT_STEPSPERREV (3325440.)
#define X_MOT_STEPSPERREV (3325952.)
//#define Y_MOT_STEPSPERREV (4394496.)
#define Y_MOT_STEPSPERREV (4394960.)
// maximal speeds in rad/s: 10deg/s by X and 8deg/s by Y
#define X_SPEED_MAX (0.17453)
#define Y_SPEED_MAX (0.13963)
// motor position to radians and back
#define X_MOT2RAD(n) (2.*M_PI * (double)n / X_MOT_STEPSPERREV)
#define Y_MOT2RAD(n) (2.*M_PI * (double)n / Y_MOT_STEPSPERREV)
#define X_RAD2MOT(r) ((int32_t)(r / 2./M_PI * X_MOT_STEPSPERREV))
#define Y_RAD2MOT(r) ((int32_t)(r / 2./M_PI * Y_MOT_STEPSPERREV))
// motor speed in rad/s and back
#define X_MOTSPD2RS(n) (X_MOT2RAD(n)/65536.*1953.)
#define X_RS2MOTSPD(r) ((int32_t)(X_RAD2MOT(r)*65536./1953.))
#define Y_MOTSPD2RS(n) (Y_MOT2RAD(n)/65536.*1953.)
#define Y_RS2MOTSPD(r) ((int32_t)(Y_RAD2MOT(r)*65536./1953.))
// adder time to seconds vice versa
#define ADDER2S(a) (a*1953.)
#define S2ADDER(s) (s/1953.)
// encoder per revolution
#define X_ENC_STEPSPERREV (67108864.)
#define Y_ENC_STEPSPERREV (67108864.)
// encoder position to radians and back
#define X_ENC2RAD(n) (2.*M_PI * (double)n / X_ENC_STEPSPERREV)
#define Y_ENC2RAD(n) (2.*M_PI * (double)n / Y_ENC_STEPSPERREV)
#define X_RAD2ENC(r) ((uint32_t)(r / 2./M_PI * X_ENC_STEPSPERREV))
#define Y_RAD2ENC(r) ((uint32_t)(r / 2./M_PI * Y_ENC_STEPSPERREV))
// encoder's tolerance (ticks)
#define YencTOL (25.)
#define XencTOL (25.)
// all need data in one
typedef struct{ // 41 bytes
uint8_t ctrlAddr; // 0 a8 + controller address
int32_t Xmot; // 1 Dec/HA motor position
int32_t Ymot; // 5
int32_t Xenc; // 9 Dec/HA encoder position
int32_t Yenc; // 13
uint8_t keypad; // 17 keypad status
xbits_t XBits; // 18
ybits_t YBits; // 19
uint8_t ExtraBits; // 20
uint16_t ain0; // 21 analog inputs
uint16_t ain1; // 23
uint32_t millis; // 25 milliseconds clock
int8_t tF; // 29 temperature (degF)
uint8_t voltage; // 30 input voltage *10 (RA worm phase?)
uint32_t XLast; // 31 Alt/Dec motor location at last Alt/Dec scope encoder location change
uint32_t YLast; // 35 Az/RA motor location at last Az/RA scope encoder location change
uint16_t checksum; // 39 checksum, H inverted
}__attribute__((packed)) SSstat;
typedef struct{
int32_t Xmot; // 0 X motor position
int32_t Xspeed; // 4 X speed
int32_t Ymot; // 8
int32_t Yspeed; // 12
uint8_t xychange; // 16 change Xbits/Ybits value
uint8_t XBits; // 17
uint8_t YBits; // 18
uint16_t checksum; // 19
} __attribute__((packed)) SSscmd; // short command
typedef struct{
int32_t Xmot; // 0 X motor position
int32_t Xspeed; // 4 X speed
int32_t Ymot; // 8
int32_t Yspeed; // 12
int32_t Xadder; // 16 - X adder
int32_t Yadder; // 20
int32_t Xatime; // 24 X adder time (1953 == 1s)
int32_t Yatime; // 28
uint16_t checksum; // 32
} __attribute__((packed)) SSlcmd; // long command
uint16_t SScalcChecksum(uint8_t *buf, int len);
void SSconvstat(const SSstat *status, mountdata_t *mountdata, struct timeval *tdat);
int SStextcmd(const char *cmd, data_t *answer);
int SSrawcmd(const char *cmd, data_t *answer);
int SSgetint(const char *cmd, int64_t *ans);
int SSsetterI(const char *cmd, int32_t ival);
int SSstop(int emerg);
int SSshortCmd(SSscmd *cmd);