/* 
 * University of Leipzig: khepera.h, v0.1b  21.9.97 22:58 
 * 
 * 
 * Copyright 1997 by University of Leipzig
 * 
 * Permission to use, copy, modify, and distribute this software for noncommercial 
 * use and without fee is hereby granted, provided that the above copyright notice 
 * appear in all copies and that both that copyright notice and this permission 
 * notice appear in supporting documentation, and that the name of the U.L. not be 
 * used in advertising or publicity pertaining to distribution of the software 
 * without specific, written prior permission. The U.L. makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is" 
 * without expressed or implied warranty. 
 * 
 * 
 * pantec@aix520.informatik.uni-leipzig.de   (Thomas Pantzer) 
 * 
 */


// Header for module Khepera, generated by p2c 1.21alpha-07.Dec.93
#ifndef K_BASIC_H
#define K_BASIC_H

//#include "floatsize.h"
#include "t_sensor.h"

#ifdef K_BASIC_G
# define vextern
#else
# define vextern extern
#endif

// #include "world.h"

// bitmasks for state variable  
#define AT_HOME                 64
#define AT_POSITION_LEFT 	32
#define SPEED_DISPLACED_LEFT  	16
#define POS_ERROR_LEFT  	8
#define AT_POSITION_RIGHT  	4
#define SPEED_DISPLACED_RIGHT  	2
#define POS_ERROR_RIGHT  	1

#define AT_POSITION		(4+32)
#define POS_ERROR		(1+8)
#define SPEED_DISPLACED		(2+16)

#define DIAMETER        	52.5	// axis, measured
#define MAX_SPEED		6.0	// your choice, up to 120
		// but your khepera might run and fall off the table ;-)

#define SENS_AVG_LEN		5.0


// #define LEFT_ROUND_TICKS	4094.47619047619047619047	  
//#define RIGHT_tick2mm		0.0785

// #define RIGHT_ROUND_TICKS	4077.28571428571428571428	// the left wheel seems to 
// #define LEFT_tick2mm		0.0822         // have a larger radius

#define RIGHT_tick2mm		0.08
#define LEFT_tick2mm		0.08         // have a larger radius


//  #define tick2mm		((RIGHT_tick2mm + LEFT_tick2mm) / 2.0)
  #define tick2mm		0.08


enum LED_Behavior {
  OFF, ON, CHANGE
};
enum SensorType {
  IRED, AMB_LIGHT
};

#define IRED	0
#define	AMBL	1

enum SensorNUM {
  LEFT, HALF_LEFT, FRONT_LEFT, FRONT_RIGHT, HALF_RIGHT, RIGHT, REAR_RIGHT,
  REAR_LEFT
};

extern  double SensorVec[8];

// sensor index
#define LEFT_S		0
#define HALF_LEFT_S	1	
#define FRONT_LEFT_S	2
#define FRONT_RIGHT_S	3
#define HALF_RIGHT_S	4
#define RIGHT_S		5
#define REAR_RIGHT_S	6
#define REAR_LEFT_S	7


// motor index
#define LEFT_M	0
#define RIGHT_M 1

enum InfoType {
  HELP, LIST, MEMBERS, NET
};


typedef struct {
                int num;
                char st[16];
               } k_response_type;
               
// commands
/*
#define CMD_CONFIG		1	
#define CMD_VERSION		2	
#define CMD_GO_POS		3	
#define CMD_SET_SPEED		4	
#define CMD_READ_SPEED		5	
#define CMD_CONFIG_PID		6	
#define CMD_SET_POS		7	
#define CMD_READ_POS		8	
#define CMD_READ_ADC		9	
#define CMD_CONFIG_ACC		10	
#define CMD_READ_FLAGS		11	
#define CMD_SET_LEDS		12	
#define CMD_READ_IR		13	
#define CMD_READ_AMBIENT	14	

#define CMD_SET_GRIP		15	
#define CMD_SET_ARM		16	
#define CMD_READ_RESISTOR	17	
#define CMD_GET_OBJECT		18	
#define CMD_READ_GRIP_POS	19	

#define CMD_READ_IMAGE		20	
#define CMD_READ_MAXL_PIX	21	
#define CMD_READ_MINL_PIX	22	
#define CMD_SET_SCAN_PERIOD	23	
*/





class t_robot_basic {
public:

  char With_Camera,With_Gripper,socket_mode,used; // booleans
  int ID; 		// ident number for multi robots


  
  // pantec added funcs
  virtual void Init(char *);
  virtual void Done(void);
  virtual void Reset(void);		// 

  virtual void Turn(double degrees);		
  virtual void Move(double millimeter);
  virtual void Lift(void);		// hands up
  virtual void Stop(void);		// all motors off

  virtual void SetMotorSpeed(double _left, double _right);  
  double GetMotorSpeed(int MotorNum);

  char DistSensor(int i);
  void UpdateSensors(void);			// reread the sensorbuffer


  // BASIC-Turret,  low level functions of the robot
  void ReadBiosVersion(char *, const int); 	// not implemented
  void ReadAddInfo(InfoType, void *);		// not implemented

  void SetWheelCounter(int left, int right);
  void TurnWheels(int left, int right);
#define ReachWheelCounter TurnWheels
  void ReadWheelCounter(int *left, int *right);

  void SetMotorSpeed(int _left, int _right);
  void GetMotorSpeed(int &L, int &R);
  void ReadLEDs(int sensor_class);				// reread the sensorbuffer

  void SetSpeedProfile(int left_maxSpeed, int right_maxSpeed, int left_acc, int right_acc);	
  void LED_Ctrl(int led_num, LED_Behavior);		
   int GetMotionCtrlFlags(void);	

  // GRIP-Turret
  void SetArmPos(int degree);
   int ReadGripOhmMeter(void);
  char CatchedObject(void);		// boolean
   int GrippedWidth(void);
   int GetArmPos(void);
  void Grip(void);
  void Release(void);           	// hands off   ;-)

  // VISION-Turret
  virtual char ImageRead(void);			// boolean
 double GetIntensity(void);
   int GetPixelMaxLight(void);
   int GetPixelMinLight(void);
  void SetScanningPeriod(int);

  // basic + vision + gripper string parsing routines
   char __GetMotorSpeed(char *st);
   char __ReadLEDs(int code, char *st2);
   char __ReadWheelCounter(char *st);
   char __GetMotionCtrlFlags(char *st);
   char __GetPixelMaxLight(char *st);
   char __GetPixelMinLight(char *st);
   char __GetIntensity(char *st);
   char __ImageRead(char *st);
   char __ImageRead2(char *st);
   char __ImageRead4(char *st);
   char __ImageRead_low(char *st);
   char __ImageRead_partial(char *st,int start, int len);
   char __GripperRead(char *st);
   char __ReadGripOhmMeter(char *st);
   char __CatchedObject(char *st);
 

//private:
	int _FD_;
  short int Image[512];	  // hold the camera pixel values  
  float image_quality; 	

//protected:
//  t_range moment[2],average[2];	
  t_SensorGroup *Ired,*Ambl,*_Ired_,*_Ambl_;
  t_Sensor	Iris;

  char device[256];	  	// filename or ( hostname + portnumber )	

  // var should reflect the internal state variables of the robot 
  int   arm_pos, 
	gr_width, 
	gr_update_flag,	
	ohm,
 	speed_left,	
	speed_right,
	minlight_idx,
	maxlight_idx,
	motion_flags;
  char  catched;
//  double iris_level;	
  int  gpl,gpr;    	

//  t_Sensor Sensor[3][64];


// protected:
//  int dark;
};


#undef vextern

#endif 

// End.












/**/
/**/
/**/
/**/
/**/
/**/
/**/
/**/
/*****  End Of File  ****************************/
