#ifndef _SharedMem_h

#define _SharedMem_h


#include "Definitions.h"


#define DATA_ROWS 1024

#define DATA_COLS 16

#define DATA_FILE "data_file.txt"

#define MEGS 10

#define MAX_LOOP_INDEX 6*MEGS



// Background write shared memory initialization constants

#define INT_STATE IDLE

#define INT_KB 'I'

#define INT_INPUT_TOGGLE LEFT






/*	This class hold the data that will be shared by the background and realtime process. 

 *	Class pointer SM (sharedMemory*) is assigned to the class. 

 *	Upon creation and mapping of the shared memory objects (look at Create_Shared_Memory()),

 *	the pointer will contain the address of the created memory objects returned by mmap(). 

 *	Thus, the class now resides in the shared memory block.

 */

class sharedMemory

{
	public:


	sharedMemory(void) {};

	~sharedMemory(void) {};


	long int ticks;
	int pid;		// control process id
	float q[NOJ];		// (simulation) robot joint position

	float dq[NOJ];		// (simulation) robot joint velocity

	float qdes[NOJ];	// desired joint position

	float qgoal[NOJ];
	float tau[NOJ];		// control torque calculated by Honda controller

	float tau_des[NOJ];	// desired joint torque
	float F[3];		// Cartesian Force command
	float Fstar[3];		// desired acceleration command
	float x[3];		// current end-effector position
	float xee[3];		// current end-effector position
	float x_des[3];		// desired end-effector position
	float time;		// time elapsed in Honda controller loop

	float jgoal[5];
	float xgoal[3];
	float qinc[5];
	float a0[3];
	float a2[3];
	float a3[3];
	float xtime;
	float tf[3];
	int jnumber;
	float freq;
	int compliance_direction;	// compliance direction in OS HOLD mode
					// 0 = x, 1 = y, 2 = z

	float force[6];
	float forcedot[6];
	float desiredForce[3];		// desired force in x,y,z

	int exit_flag;		// exit flag

	int userExit;		// user exit flag

	int userMode;		// user control mode; set by user.cpp
	int miniMode;
	pthread_mutex_t mutex;	// mutual exclusion protection
};

class SM_Functions

{

	public:



	/*	Function: Create_Shared_Memory

	 *	Usage: Create_Shared_Memory()

	 *	-------------------------------

	 *	This functions create a new shared memory block used to communicate between background and realtime

	 *	processes. After creation, it sets the size of the memory block and map it.

	 */



//	void Create_Shared_Memory(void);

	sharedMemory* Create_Shared_Memory(void);



	/* Function: Open_Shared_Memory

	 * Usage: Open_Shared_Memory();

	 *------------------------

	 * This function opens the shared memory area used by the realtime and

	 * background control processes

	 */



	void Open_Shared_Memory(void);



	/* Function: Map_Shared_Memory

	 * Usage: Map_Shared_Memory();

	 *------------------------

	 * This function maps the shared memory object area and assigns it to the class pointer

	 */



//	void Map_Shared_Memory(void);

//	sharedMemory* Map_Shared_Memory(void);

	sharedMemory* Map_Shared_Memory(int address);



	/* Function: Release_Shared_Memory

	 * Usage: Release_Shared_Memory(void);

	 * ------------------------------------

	 * This function is used to unlink and close the shared memory areas used to

	 * communicate between the realtime and background processes

	 */



	void Release_Shared_Memory(void);



	/* Function: Initialize_Shared_Memory

	 * Usage: Initialize_Shared_Memory();

	 * ------------------------------------

	 * This function is used to initialize the variables of the shared memory

	 * object used to communicate between the background and realtime processes

	 */



	void Init_Shared_Memory(void);



	/* Function: Write_To_SM

	 * Usage: Write_To_SM(localSM);

	 * ------------------------------------------------------

	 * This function is used to write a class object pointed by localSM  of type

	 * sharedMemory to the background process portion of the shared memory area

	 */



	void Write_To_SM(sharedMemory* localSM);



	/* Function: Init_Local_SM

	 * Usage: Init_Local_SM();

	 * -----------------------------

	 * This function is used to initialize the local copy of 

	 * shared memory that mirrors the shared memory object.

	 * The function allocates space and copies the contents of the shared memory

	 */



	sharedMemory* Init_Local_SM(sharedMemory* localSM);





	/* Function: Read__SM

	 * Usage: Read_Background_SM(localSM);

	 * -----------------------------------

	 * This function copies the background process portion of shared memory into

	 * a local copy.  The function assumes that space has been allocated for localSM

	 */



	sharedMemory* Read_SM (sharedMemory* localSM);



};





#endif







