# 1 "../laserPowerControl.st"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "../laserPowerControl.st"
# 33 "../laserPowerControl.st"
program laserPowerControl

option +r;
option +c;
option +d;

%%#include "stdio.h"
%%#include "string.h"
%%#include "math.h"
%%#include "epicsTime.h"
# 72 "../laserPowerControl.st"
double laserPowerSetPoint;
assign laserPowerSetPoint to "{FBCK}:PCTRL";
monitor laserPowerSetPoint;

double cameraPowerSetPoint;
assign cameraPowerSetPoint to "{FBCK}:CCTRL";
monitor cameraPowerSetPoint;

string statusStr;
assign statusStr to "{FBCK}:PCSTATUS";

short fbckEnable;
assign fbckEnable to "{ENABLE}";
monitor fbckEnable;


double laserWaveplateAngle_val;
assign laserWaveplateAngle_val to "{LASR_WPLT}.VAL";

short laserWaveplateAngle_dmov;
assign laserWaveplateAngle_dmov to "{LASR_WPLT}.DMOV";
monitor laserWaveplateAngle_dmov;

double laserWaveplateAngle_max;
assign laserWaveplateAngle_max to "{LASR_ANG_MAX}";
monitor laserWaveplateAngle_max;


double cameraWaveplateAngle_val;
assign cameraWaveplateAngle_val to "WPLT:IN20:181:VCC_ANGLE.VAL";

short cameraWaveplateAngle_dmov;
assign cameraWaveplateAngle_dmov to "WPLT:IN20:181:VCC_ANGLE.DMOV";
monitor cameraWaveplateAngle_dmov;

double cameraWaveplateAngle_max;
assign cameraWaveplateAngle_max to "WPLT:IN20:181:ANG_MAX";
monitor cameraWaveplateAngle_max;




evflag eventLaserPowerSetPointChange;
evflag eventCameraPowerSetPointChange;

sync laserPowerSetPoint eventLaserPowerSetPointChange;
sync cameraPowerSetPoint eventCameraPowerSetPointChange;




double newLaserPower;
double newCameraPower;

double oldLaserPower;
double oldCameraPower;

double cosLaserAngle;
double cosCameraAngle;

double newLaserAngle;
double newCameraAngle;

int ctrlFlag;




int exceptionCode;
# 151 "../laserPowerControl.st"
ss laserPowerControlFSM {



    state init {

        entry {
            if(0) printf("laserPowerControlFSM:init - entry\n");

            newLaserPower = 0.0;
            newCameraPower = 0.0;
            oldLaserPower = 0.0;
            oldCameraPower = 0.0;
            cosLaserAngle = 0.0;
            cosCameraAngle = 0.0;
            newLaserAngle = 0.0;
            newCameraAngle = 0.0;
            ctrlFlag = 0;
            exceptionCode = 0;


            if(laserPowerSetPoint <= 0) {
                if(pvGet(laserWaveplateAngle_val) == pvStatOK &&
                   pvGet(cameraWaveplateAngle_val) == pvStatOK) {
                    cosLaserAngle = cos(2 * ((laserWaveplateAngle_val - laserWaveplateAngle_max) * 3.1415926 / 180.0));
                    cosCameraAngle = cos(2 * ((cameraWaveplateAngle_val - cameraWaveplateAngle_max) * 3.1415926 / 180.0));

                    laserPowerSetPoint = 100.0 * cosLaserAngle * cosLaserAngle;
                    cameraPowerSetPoint = 100.0 * cosCameraAngle * cosCameraAngle;


                    cameraPowerSetPoint = cameraPowerSetPoint * laserPowerSetPoint / 100.0;

                    pvPut(laserPowerSetPoint);
                    pvPut(cameraPowerSetPoint);

printf("Set the power sp values from the angles: laserPower = %lf, cameraPower = %lf\n", laserPowerSetPoint, cameraPowerSetPoint);
                }
            }
     printf("Starting position = %lf\n", laserWaveplateAngle_val);
        }


        when(pvConnectCount() != pvChannelCount()) {
            exceptionCode = 1;
            if(0) printf("laserPowerControlFSM:init - transient to exceptionHandling\n");
        } state exceptionHandling

        when(pvConnectCount() == pvChannelCount()) {
            if(0) printf("laserPowerControlFSM:init - transient to idle\n");
        } state idle
    }




    state idle {
        when(fbckEnable && (efTestAndClear(eventLaserPowerSetPointChange) || efTestAndClear(eventCameraPowerSetPointChange)) &&
             laserPowerSetPoint >= 0 && cameraPowerSetPoint >= 0) {;
            if(0) printf("laserPowerControlFSM:idle - transient to calculation\n");
        } state calcuation
    }




    state calcuation {
        entry {
            if(0) printf("laserPowerControlFSM:calculation - entry\n");


            ctrlFlag = 0;


            newLaserPower = laserPowerSetPoint;

            if(newLaserPower == 0) newCameraPower = 100.0;
            else newCameraPower = 100.0 * cameraPowerSetPoint / newLaserPower;

            newLaserPower = (((100) > (newLaserPower)) ? (newLaserPower) : (100));
            newLaserPower = (((0) < (newLaserPower)) ? (newLaserPower) : (0));
            newCameraPower = (((100) > (newCameraPower)) ? (newCameraPower) : (100));
            newCameraPower = (((0) < (newCameraPower)) ? (newCameraPower) : (0));


            if(pvGet(laserWaveplateAngle_val) == pvStatOK &&
               pvGet(cameraWaveplateAngle_val) == pvStatOK) {
                cosLaserAngle = cos(2 * ((laserWaveplateAngle_val - laserWaveplateAngle_max) * 3.1415926 / 180.0));
                cosCameraAngle = cos(2 * ((cameraWaveplateAngle_val - cameraWaveplateAngle_max) * 3.1415926 / 180.0));

                oldLaserPower = 100.0 * cosLaserAngle * cosLaserAngle;
                oldCameraPower = 100.0 * cosCameraAngle * cosCameraAngle;


                newLaserAngle = laserWaveplateAngle_max - 0.5 * ((acos(sqrt(newLaserPower / 100.0))) * 180.0 / 3.1415926);
                newCameraAngle = cameraWaveplateAngle_max - 0.5 * ((acos(sqrt(newCameraPower / 100.0))) * 180.0 / 3.1415926);

printf("newLaserPower = %lf, oldLaserPower = %lf, currentAngle = %lf\n", newLaserPower, oldLaserPower, laserWaveplateAngle_val);


                if(newLaserPower > oldLaserPower) ctrlFlag = 2;
                else ctrlFlag = 1;
            }
        }


        when(ctrlFlag == 1) {
            if(0) printf("laserPowerControlFSM:calculation - transient to laserMotorMoving\n");
        } state laserMotorMoving


        when(ctrlFlag == 2) {
            if(0) printf("laserPowerControlFSM:calculation - transient to cameraMotorMoving\n");
        } state cameraMotorMoving


        when(ctrlFlag <= 0 || ctrlFlag > 2) {
            exceptionCode = 2;
            if(0) printf("laserPowerControlFSM:calculation - transient to exceptionHandling\n");
        } state exceptionHandling
    }




    state cameraMotorMoving {

        entry {
            if(0) printf("laserPowerControlFSM:cameraMotorMoving - entry\n");
            cameraWaveplateAngle_val = newCameraAngle;
            pvPut(cameraWaveplateAngle_val);
        }


        when(cameraWaveplateAngle_dmov == 1 && ctrlFlag == 2) {
            if(0) printf("laserPowerControlFSM:cameraMotorMoving - transient to laserMotorMoving\n");
        } state laserMotorMoving


        when(cameraWaveplateAngle_dmov == 1 && ctrlFlag == 1) {
            exceptionCode = 0;
            if(0) printf("laserPowerControlFSM:cameraMotorMoving - transient to exception handling (succeed)\n");
        } state exceptionHandling


        when(delay(60)) {
            exceptionCode = 4;
            if(0) printf("laserPowerControlFSM:cameraMotorMoving - transient to exception handling (failed)\n");
        } state exceptionHandling
    }




    state laserMotorMoving {

        entry {
            if(0) printf("laserPowerControlFSM:laserMotorMoving - entry\n");
            laserWaveplateAngle_val = newLaserAngle;
            pvPut(laserWaveplateAngle_val);
        }


        when(laserWaveplateAngle_dmov == 1 && ctrlFlag == 1) {
            if(0) printf("laserPowerControlFSM:laserMotorMoving - transient to cameraMotorMoving\n");
        } state cameraMotorMoving


        when(laserWaveplateAngle_dmov == 1 && ctrlFlag == 2) {
            exceptionCode = 0;
            if(0) printf("laserPowerControlFSM:laserMotorMoving - transient to exception handling (succeed)\n");
        } state exceptionHandling


        when(delay(60)) {
            exceptionCode = 3;
            if(0) printf("laserPowerControlFSM:laserMotorMoving - transient to exception handling (failed)\n");
        } state exceptionHandling
    }




    state exceptionHandling {
        entry {
            if(0) printf("laserPowerControlFSM:exceptionHandling - entry\n");

            if(exceptionCode == 0) {
                sprintf(statusStr, "OK");
                if(0) printf("laserPowerControlFSM:exceptionHandling - Motor move OK\n");

            } else if(exceptionCode == -1) {
                sprintf(statusStr, "Failure");
                if(0) printf("laserPowerControlFSM:exceptionHandling - Failure\n");

            } else if(exceptionCode == 1) {
                sprintf(statusStr, "Not all channel connected");
                if(0) printf("laserPowerControlFSM:exceptionHandling - Not all channel connected\n");

            } else if(exceptionCode == 2) {
                sprintf(statusStr, "Undfined motor instruction");
                if(0) printf("laserPowerControlFSM:exceptionHandling - Undfined motor instruction\n");

            } else if(exceptionCode == 3) {
                sprintf(statusStr, "Laser motor failure");
                if(0) printf("laserPowerControlFSM:exceptionHandling - Laser motor failure\n");

            } else if(exceptionCode == 4) {
                sprintf(statusStr, "Camera motor failure");
                if(0) printf("laserPowerControlFSM:exceptionHandling - Camera motor failure\n");

            } else {
                sprintf(statusStr, "Unknown exceptions");
                if(0) printf("laserPowerControlFSM:exceptionHandling - Unknown exceptions\n");
            }

            pvPut(statusStr);
        }

        when() {
            if(0) printf("laserPowerControlFSM:exceptionHandling - transient to idle\n");
        } state idle
    }
}
