#include "Shared/RobotInfo.h"
#include "Behaviors/StateMachine.h"
#include <math.h>

#if defined(TGT_HAS_ARMS) && defined(TGT_HAS_HEAD)

#include "Behaviors/Nodes/PostureNode.h"
#include "Behaviors/Transitions/EventTrans.h"
#include "Motion/MotionPtr.h"

$nodeclass GripperPointEllipse : StateNode {

	//Make the arm track the camera
	$nodeclass ArmMover : PostureNode : doStart {

		//Calculate the angles for the arm of the robot & position
		float curPan = state->outputs[HeadOffset+PanOffset];
		float maxTurn = 0.5;
		float fractionTurn = curPan/maxTurn;
		float radTurn = (M_PI/2) * fractionTurn;
		if (radTurn > M_PI/2) radTurn = M_PI/2;
		else if (radTurn < -M_PI/2) radTurn = -M_PI/2;
		float xPos = 200 + 100*(1 - cos(radTurn));
		float yPos = 100 * (sin(radTurn));

		//Get the arm to move to the correct position in field
	 	fmat::Column<3> targetBase = fmat::pack(xPos,yPos,0);
		fmat::Quaternion oriTgt = fmat::Quaternion::aboutZ(-radTurn);
		unsigned int link = GripperFrameOffset;
		fmat::Quaternion oriOffset = fmat::Quaternion::identity();
		fmat::Column<3> noOffset = fmat::pack(0, 0, 0);
		getMC()->solveLink(targetBase, oriTgt, link, noOffset, oriOffset);
	}

	virtual void setup() {
			// We make the posture motion command here and supply it to the
			// PostureNode via setMC() so that it only has to be registered
			// with the MotionManager once. Registering and deregistering a
			// PostureMC inside a tight sensor loop is inefficient and leads
			// to crashes.
			MotionManager::MC_ID posturemc = addMotion(MotionPtr<PostureMC>());

		$statemachine{
	  		startnode: PostureNode("beginPos.pos") =C=> armmove: ArmMover[setMC(posturemc)] =E(sensorEGID)=> armmove
		}
	}
}
REGISTER_BEHAVIOR(GripperPointEllipse);
#endif
