// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved


// This software makes use of tools and libraries obtained from open source projects or released for

// use by relevant hardware manufactures. However, this software is NOT a part of any open source project.

// It is released only on a "need to know" basis for beta testers of the "RiceCNC Interpolation Engine".

// Recipents of this source code must respect the confidential nature of this software and prevent any

// distribution that could result in counterfeit copies of the "RiceCNC Interpolation Engine".


// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved

#include <MachineCommands.h>

#include <Machine.h>

#include <AdjustProgramPosition.h>

#include <Axis.h>

#include <SplineCommand.h>

#include <ProbingSplineCommand.h>

#include <SlaveModeCommand.h>

#include <MachineSplineCommand.h>

#include <InterpolationTimer.h>


void

     Machine::positionControl(

          char*          commandPtr,

          String*          msgPtr ) {


     char* data     = commandPtr + 1;


     switch ( *commandPtr ) {

     

// in-band - acknowledge immediately

          case ADJUST_PROGRAM_POSITION: // not used

          queueCommand(

               new AdjustProgramPosition( data ) );

          *msgPtr

               +=     String( POSITION_CONTROL )

               +     String( ADJUST_PROGRAM_POSITION );

          break;


// out-of-band

          case ENTER_SLAVE_MODE :

          enterSlaveMode(

               data,

               msgPtr );

          break;


          case CREATE_SPLINE :

          createSpline(

               data,

               msgPtr );

          break;


          case CREATE_PROBING_SPLINE :

          createProbingSpline(

               data,

               msgPtr );

          break;


          case TRACK_PAD_JOG :

          trackPadJog(

               data,

               msgPtr );

          break;


     // Move in machine space

          case TRAVERSE_MACHINE :

          createMachineSpline(

               data,

               msgPtr );

          break;


          case TRACK_PAD_JOG_MACHINE :

          trackPadJogMachine(

               data,

               msgPtr );

          break;


          case SET_AXIS_VELOCITY :

          setVelocity(

               data,

               msgPtr );

          break;


          default :

          *msgPtr

               +=     *commandPtr

               +     "E Invalid Machine Command - Ignored"; }; };



void

     Machine::setVelocity(

          char*          data,

          String*          msgPtr ) {


     *msgPtr

          +=     String( POSITION_CONTROL )

          +     String( SET_AXIS_VELOCITY );


     Axis* axisPtr = findAxis(

          *data,

          msgPtr );

     if ( axisPtr )

          axisPtr->setTargetVelocityWithData(

               data + 1,

               msgPtr ); };



void

     Machine::enterSlaveMode(

          char*          data,

          String*          msgPtr ) {


     *msgPtr

          +=     String( POSITION_CONTROL )

          +     String( ENTER_SLAVE_MODE );


     queueCommand(

          new SlaveModeCommand( data, firstAxis, msgPtr ) ); };



void

     Machine::createSpline(

          char*          data,

          String*          msgPtr ) {


     *msgPtr

          +=     String( POSITION_CONTROL )

          +     String( CREATE_SPLINE );


     queueCommand(

          new Spline( data, firstAxis, msgPtr ) ); };


void

     Machine::createProbingSpline(

          char*          data,

          String*          msgPtr ) {


     *msgPtr

          +=     String( POSITION_CONTROL )

          +     String( CREATE_PROBING_SPLINE );


     queueCommand(

          new ProbingSplineCommand( data, firstAxis, msgPtr ) ); };


void

     Machine::createMachineSpline( char* data, String* msgPtr ) {


     *msgPtr

          +=     String( POSITION_CONTROL )

          +     String( TRAVERSE_MACHINE );


     queueCommand(

          new MachineSpline( data, firstAxis, msgPtr ) ); };



void

     Machine::trackPadJog(

          char*          data,

          String*          msgPtr ) {


     *msgPtr

          +=     String( POSITION_CONTROL )

          +     String( TRACK_PAD_JOG );


     // Same as createSpline but abort current interpolation

     InterpolationTimer::disableInterpolationInterrupts();

          if ( currentCommand )

               currentCommand->trackPadJogAbort( this );

     InterpolationTimer::enableInterpolationInterrupts();


     queueCommand( new Spline( data, firstAxis, msgPtr ) ); };



void

     Machine::trackPadJogMachine(

          char*          data,

          String*          msgPtr ) {


     *msgPtr

          +=     String( POSITION_CONTROL )

          +     String( TRACK_PAD_JOG_MACHINE );


     // Same as createMachineSpline but abort current interpolation

     InterpolationTimer::disableInterpolationInterrupts();

          if ( currentCommand )

               currentCommand->trackPadJogAbort( this );

     InterpolationTimer::enableInterpolationInterrupts();


     queueCommand( new MachineSpline( data, firstAxis, msgPtr ) ); };