Commit f7c2892c authored by gdargaud's avatar gdargaud
Browse files

Moved globals to struct

parent ed55f1f1
......@@ -42,16 +42,18 @@
#include "UseColors.h"
#include "TB_Phytron.h"
#if defined(REMOTE) and REMOTE==1 // Use "make REMOTE=1"
// When in use through a ssh port redirection such as:
// ssh -YCL 5906:localhost:5900 -L 5001:192.168.0.20:5001 -L 22222:192.168.0.31:22222 -L 10011:192.168.0.11:23 -L 10012:192.168.0.12:23 -L 10013:192.168.0.13:23 ccob@lpsc2006x
char Phytron_IP[]="127.0.0.1";
int Phytron_Port=22222;
#else // When in direct use
char Phytron_IP[]="192.168.0.31"; // This is defined by the dhcp server configuration. See /etc/dhcp/dhcpd.conf file
int Phytron_Port=22222;
#endif
tPhytron Phytron={
.IP="192.168.0.31", // This is defined by the dhcp server configuration. See /etc/dhcp/dhcpd.conf file
.Port=22222,
.msWait=10, // Min delay between command and reply and new command. 50ms seems safe enough
.msMotionPolling=500, // Same as above, but higher value to limit the number of polling messages
.SpeedDiv=10, // Range: 1 to 256. Default is 6 or 10. 1 is too fast and might break the switch
.MoveInDiode_Pos=34, // Precise angle where the diode is in the middle of the beam
.OpMin=0, .OpMax=70, // Operational motion limits (in deg)
.MainDir=+1, // +1 if the homing should go for the positive direction, -1 otherwise.
// Don't get this wrong or you will BREAK the support
.UseEncoder=0
};
// So that the various instruments have different alignments to make their logging more readable when intermixed
static char Tabs[]="\t\t";
......@@ -75,19 +77,10 @@ static int Display=D_ALL; // Global to skip displaying some commands or replie
static int sock=0; // Socket
int Phytron_msWait=10; // Min delay between command and reply and new command. 50ms seems safe enough
#define msleep(ms) usleep((ms)*1000)// ms sleep
int Phytron_msMotionPolling=500; // Same as above, but higher value to limit the number of polling messages
int Phytron_SpeedDiv=10; // Range: 1 to 256. Default is 6 or 10. 1 is too fast and might break the switch
double Phytron_MoveInDiode_Pos=34; // Precise angle where the diode is in the middle of the beam
double Phytron_OpMin=0, Phytron_OpMax=70;// Operational motion limits (in deg)
int Phytron_MainDir=+1; // +1 if the homing should go for the positive direction, -1 otherwise.
// Don't get this wrong or you will BREAK the support
int Phytron_UseEncoder=0;
static int Enabled=0;
static char LastErrMsg[1024]="";
#define PrintErr(f_, ...) sprintf(LastErrMsg, (f_), ##__VA_ARGS__), printf(BLD RED "%s%s\n" NRM, Tabs, LastErrMsg)
#define msleep(ms) usleep((ms)*1000)// ms sleep
static void (*PreviousSignalHandlerPipe)(int) = NULL;
static void (*PreviousSignalHandlerInt )(int) = NULL;
......@@ -120,13 +113,13 @@ int PH_Init(void) {
printf("%sSocket created for Phytron\n", Tabs);
// Prepare the sockaddr_in structure
server.sin_addr.s_addr = inet_addr(Phytron_IP);
server.sin_addr.s_addr = inet_addr(Phytron.IP);
server.sin_family = AF_INET;
server.sin_port = htons( Phytron_Port );
server.sin_port = htons( Phytron.Port );
// Connect to remote server
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
PrintErr("Connect to %s:%d failed: %s", Phytron_IP, Phytron_Port, strerror(errno));
PrintErr("Connect to %s:%d failed: %s", Phytron.IP, Phytron.Port, strerror(errno));
return errno;
}
......@@ -178,7 +171,7 @@ int PH_SendCommand(char* Reply, const char* Command, ... ) {
}
if (Display!=D_NONE)
printf("Sent :%s%s\n", Tabs, Cmd);
msleep(Phytron_msWait);
msleep(Phytron.msWait);
// Receive a reply from the server
// if (recv(sock, Reply, MAX_REPLY, 0) < 0) {
......@@ -202,7 +195,7 @@ int PH_SendCommand(char* Reply, const char* Command, ... ) {
if (Display==D_ALL)
printf("Reply:%s%s\n", Tabs, Reply);
msleep(Phytron_msWait);
msleep(Phytron.msWait);
return 0;
}
......@@ -477,6 +470,7 @@ int PH_SetParamValue(int Param, int Val) {
}
/////////////////////////////////////////////////////////////////////
/// HIPAR Param/Only valid for 3 and 19
/// HIRET 0 if no error
/////////////////////////////////////////////////////////////////////
int PH_SetParamValueF(int Param, double Val) {
......@@ -503,7 +497,8 @@ int PH_GetParamValue(int Param) {
}
/////////////////////////////////////////////////////////////////////
/// HIPAR Some values are floats. Currently only 3 and 19
/// HIFN Some values are floats.
/// HIPAR Param/Only valid for 3 and 19
/// HIRET Parameter value is returned
/////////////////////////////////////////////////////////////////////
double PH_GetParamValueF(int Param) {
......@@ -654,7 +649,7 @@ int main(int argc, const char* argv[]) {
"Pos:\tAbsolute positions to move the motor to (numerical value, in degrees)\n"
"@Cmd:\tArbitrary commands to send to the controller (must follow a '@'). No sanity check is performed\n"
"Currently %susing the encoder\n"
, argv[0], Phytron_UseEncoder?"NOT ":"");
, argv[0], Phytron.UseEncoder?"NOT ":"");
return 1;
}
......@@ -668,7 +663,7 @@ int main(int argc, const char* argv[]) {
// Ret|=PH_ResetAxis(); // not necessary
Ret|=PH_DoGetGeneralStatus();
Ret|=PH_SetParamValue(45, 4 /*FIXME Phytron_SpeedDiv */); // Full step. Default was 1/6 ?
Ret|=PH_SetParamValue(45, 4 /*FIXME Phytron.SpeedDiv */); // 1 for full step. Default was 1/6 ?
Ret|=PH_SetParamValue(2, 1); // Measuring unit of movement - Step
// Ret|=PH_SetParamValue(2, 4); // Measuring unit of movement - Degree - DOES NOT SEEM DO ANYTHING
......@@ -686,15 +681,15 @@ int main(int argc, const char* argv[]) {
// It works but it doesn't make sense: it should be the opposite
#endif
Ret|=PH_MoveToLimit(Phytron_MainDir); // If no contactor, will stop after a timeout (18s I think. It's one of the parameters)
do msleep(Phytron_msMotionPolling); while (PH_GetStatusAxes());
Ret|=PH_MoveToLimit(Phytron.MainDir); // If no contactor, will stop after a timeout (18s I think. It's one of the parameters)
do msleep(Phytron.msMotionPolling); while (PH_GetStatusAxes());
// PH_DbgGetPosition(Q[i]);
PAUSE;
Ret|=PH_SetParamValueF(19, 0.); // Reset EL0P
#if 0 // Check the effect of the resolution. It's important at low angles because of truncation
PH_SetParamValue(34, Phytron_UseEncoder);
PH_SetParamValue(34, Phytron.UseEncoder);
double POS=90.;
Ret|=PH_SetParamValue(45, 1); // Full step. Default was 4
TB_Phytron_MoveAbs(POS);
......@@ -718,18 +713,18 @@ int main(int argc, const char* argv[]) {
printf("\nArgv:%s%s\n", Tabs, argv[i]);
if (argv[i][0]=='@') { // If @ as first char: arbitrary command
PH_SendCommand(Reply, "%s", argv[i]+1);
//msleep(Phytron_msMotionPolling);
//msleep(Phytron.msMotionPolling);
} else { // Otherwise assume numerical absolute moves
Ret|=PH_AbsPosition(atof(argv[i]));
if (Ret) break;
do msleep(Phytron_msMotionPolling); while (PH_GetStatusAxes());
do msleep(Phytron.msMotionPolling); while (PH_GetStatusAxes());
PH_DbgGetPosition(P[i]);
}
PAUSE;
}
Ret|=PH_MoveToLimit(Phytron_MainDir); // If no contactor, will stop after a timeout (18s I think. It's one of the parameters)
do msleep(Phytron_msMotionPolling); while (PH_GetStatusAxes());
Ret|=PH_MoveToLimit(Phytron.MainDir); // If no contactor, will stop after a timeout (18s I think. It's one of the parameters)
do msleep(Phytron.msMotionPolling); while (PH_GetStatusAxes());
// Now with encoder
// This works, but there seems to be no benefit at standard speeds
......@@ -738,7 +733,7 @@ int main(int argc, const char* argv[]) {
for (i=1; i<argc; i++) {
Ret|=PH_AbsPosition(atof(argv[i]));
if (Ret) break;
do msleep(Phytron_msMotionPolling); while (PH_GetStatusAxes());
do msleep(Phytron.msMotionPolling); while (PH_GetStatusAxes());
PH_DbgGetPosition(Q[i]);
PAUSE;
}
......@@ -809,19 +804,19 @@ int TB_Phytron_Init(void) {
if ((Ret=PH_Reset())) return Ret;
if ((Ret=PH_DoGetGeneralStatus())) return Ret;
if ((Ret=PH_SetParamValue(45, Phytron_SpeedDiv))) return Ret;// Full step. Default was 1/6 ?
if ((Ret=PH_SetParamValue(45, Phytron.SpeedDiv))) return Ret;// Full step. Default was 1/6 ?
if ((Ret=PH_SetParamValue(2, 1))) return Ret; // Measuring unit of movement - Step
if ((Ret=PH_SetParamValue(39, GearReduction))) return Ret; // Encoder conversion factor - DOES NOT SEEM TO DO ANYTHING
if ((Ret=PH_SetParamValue (34, Phytron_UseEncoder))) return Ret;
if ((Ret=PH_SetParamValue (34, Phytron.UseEncoder))) return Ret;
// Check the effect of limits/timeout on initiator motion
Ret|=PH_SetParamValue(23, D2P(90)); // Axis + limitation
Ret|=PH_SetParamValue(24, D2P(0)); // Axis - limitation
// It works but it doesn't make sense: it should be the opposite
if ((Ret=PH_MoveToLimit(Phytron_MainDir))) return Ret;
do msleep(Phytron_msMotionPolling); while (PH_GetStatusAxes());
if ((Ret=PH_MoveToLimit(Phytron.MainDir))) return Ret;
do msleep(Phytron.msMotionPolling); while (PH_GetStatusAxes());
if ((Ret=PH_SetParamValueF(19, 0.))) return Ret; // Reset EL0P
printf(BLD GRN "================== PHYTRON MOTOR READY ==================\n" NRM);
......@@ -867,7 +862,7 @@ char* TB_Phytron_Status(void) {
sprintf(String+strlen(String), "Axis limit pos: +:%.1fdeg, -:%.1fdeg\n", P2D(PH_GetParamValue(23)), P2D(PH_GetParamValue(24)));
// sprintf(String+strlen(String), "Hardware limits: -Inf..+Inf\n"); // TODO: check if there is a parameter for that
sprintf(String+strlen(String), "Software limits: %.0f..%.0f", Phytron_OpMin, Phytron_OpMax);
sprintf(String+strlen(String), "Software limits: %.0f..%.0f", Phytron.OpMin, Phytron.OpMax);
return String;
}
......@@ -879,12 +874,12 @@ char* TB_Phytron_Status(void) {
int TB_Phytron_MoveAbs(double Angle) {
int Ret;
if (!Enabled) { PrintErr("The system is not ready, call TB_Phytron_Init() before"); return 1; }
if (Angle<Phytron_OpMin or Phytron_OpMax<Angle) {
if (Angle<Phytron.OpMin or Phytron.OpMax<Angle) {
PrintErr("Attempt to move to %.1f out of allowed range %.0f..%.0fdeg",
Angle, Phytron_OpMin, Phytron_OpMax); return 1; }
Angle, Phytron.OpMin, Phytron.OpMax); return 1; }
// TODO: sanity check, but not really necessary as we have full 360deg motion
if ((Ret=PH_AbsPosition(Angle))) return Ret;
while (PH_GetStatusAxes()) msleep(Phytron_msMotionPolling);
while (PH_GetStatusAxes()) msleep(Phytron.msMotionPolling);
// PH_DbgGetPosition(P[i]);
return 0;
}
......@@ -894,7 +889,7 @@ int TB_Phytron_MoveAbs(double Angle) {
/// HIPAR SD/1 for highest speed, 255 for lowest
///////////////////////////////////////////////////////////////////////////////
int TB_Phytron_SpeedDiv(int SD) {
return PH_SetParamValue(45, Phytron_SpeedDiv = (SD<1 ? 1 : SD>256 ? 256 : SD));
return PH_SetParamValue(45, Phytron.SpeedDiv = (SD<1 ? 1 : SD>256 ? 256 : SD));
}
///////////////////////////////////////////////////////////////////////////////
......@@ -902,7 +897,7 @@ int TB_Phytron_SpeedDiv(int SD) {
/// HIPAR Do/0 to move the diode to the rest position, 1 to move it in the path of the beam
///////////////////////////////////////////////////////////////////////////////
extern int TB_Phytron_MoveInDiode(int Do) {
return TB_Phytron_MoveAbs(Do ? Phytron_MoveInDiode_Pos : 0);
return TB_Phytron_MoveAbs(Do ? Phytron.MoveInDiode_Pos : 0);
}
///////////////////////////////////////////////////////////////////////////////
......
#ifndef __TB_PHYTRON_H
#define __TB_PHYTRON_H
extern char Phytron_IP[80]; // This is defined by the dhcp server configuration. See /etc/dhcp/dhcpd.conf file
extern int Phytron_Port; // Note that the Phytron opens several ports for various uses
extern int Phytron_msWait; // Min delay between command and reply and new command. 50ms seems safe enough
extern int Phytron_msMotionPolling; // Same as above, but higher value to limit the number of polling messages
extern int Phytron_SpeedDiv; // Speed divisor (1-fastest to 256-slowest)
extern double Phytron_MoveInDiode_Pos; // Precise angle where the diode is in the middle of the beam
extern double Phytron_OpMin, Phytron_OpMax; // Operational motion limits (in deg)
extern int Phytron_MainDir; // +1 if the homing should go for the positive direction, -1 otherwise
extern int Phytron_UseEncoder; // 1 to use encoder (if available). Must be set BEFORE init()
// Some (like IP and Port) must be set before calling PH_Init()
typedef struct sPhytron {
char IP[80]; // This is defined by the dhcp server configuration. See /etc/dhcp/dhcpd.conf file
int Port; // Note that the Phytron opens several ports for various uses
int msWait; // Min delay between command and reply and new command. 50ms seems safe enough
int msMotionPolling; // Same as above, but higher value to limit the number of polling messages
int SpeedDiv; // Speed divisor (1-fastest to 256-slowest)
double MoveInDiode_Pos; // Precise angle where the diode is in the middle of the beam
double OpMin, OpMax; // Operational motion limits (in deg)
int MainDir; // +1 if the homing should go for the positive direction, -1 otherwise
int UseEncoder; // 1 to use encoder (if available). Must be set BEFORE init()
} tPhytron;
extern tPhytron Phytron;
// Low level commands
extern int PH_Init(void);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment