#ifndef _CANBUS_H_
#define _CANBUS_H_

/* Definations
 * ------------------------------------------------------------------------------
 */

/* Don't modify */
#define CAN_SPEED_1M    (-1)
#define CAN_SPEED_500K  (-2)
#define CAN_SPEED_250K  (-3)
#define CAN_SPEED_125K  (-4)
#define CAN_SPEED_100K  (-5)
#define CAN_SPEED_62K   (-6)
#define CAN_SPEED_50K   (-7)

/* You can modify these but remember to change the speed of 
 * canbus devices also 
 */ 
#define	CAN_BUS_SPEED_0   CAN_SPEED_500K 
#define	CAN_BUS_SPEED_1   CAN_SPEED_500K 

#define CAN_BUS_NO_MSG  (-1)

#define CAN_USE_CHANNEL_NONE  0
#define CAN_USE_CHANNEL_1     1
#define CAN_USE_CHANNEL_2     2
#define CAN_USE_CHANNEL_ALL   10

#define CAN_ID_DRIVER_CNTR1_OUT   0x50
#define CAN_ID_DRIVER_CNTR2_OUT   0x51


/* Driver status
 * ----------------------------------------------
 * 2 bytes
 */
/* Id */
#define CAN_ID_DRIVER_STATUS  0x60  

/* Motor Currents
 * ----------------------------------------------
 */
/* Id */
#define CAN_ID_MOTOR_CURRENTS           0x12
/* Input is -5V ... 5V = -10.6A ... 10.6A
 * Scale is 10.6/(2^15) = 0.000323486
 */
#define CAN_MOTOR_CURRENT_SCALE         0.000323486
#define CAN_MOTOR_CURRENT_SCALE_BITEX   1000

/* Temperatures
 * ----------------------------------------------
 */
/* Id */
#define CAN_ID_TEMPERATURES   0x11
/* Input range is -50deg ... +150 deg 
 * Scale factor should be  200/(2^16) = 0.003051758
 * CANSAS says its 0.003057960 
 */ 
#define CAN_TEMP_SCALE        0.003057960 
#define CAN_TEMP_OFFSET       50.0
#define CAN_TEMP_SCALE_BITEX  10.0

/* Tachos
 * ----------------------------------------------
 */
/* Id */
#define CAN_ID_TACHOS       0x10

/* Input range is -5V ... +5V (16bit) 
 * Scale factor is  0.000152593
 */ 
#define CAN_AZ_TACHO_SCALE  0.000152593
#define CAN_EL_TACHO_SCALE  0.000152593

#define CAN_AZ_TACHO_OFFSET 0.010
#define CAN_EL_TACHO_OFFSET 0.000

#define CAN_AZ_TACHO_MAX    3.34
#define CAN_EL_TACHO_MAX    3.34

/* Encoders
 * ----------------------------------------------
 */
/* Id */
#define CAN_ID_AZ_ENCODER     0x21
#define CAN_ID_EL_ENCODER     0x41
/* Input range is -180 ... 180 
 * Scale factor is 360/(2^14) = 0.021972656 
 */ 
#define CAN_AZ_ENCODER_SCALE  0.021972656  
#define CAN_EL_ENCODER_SCALE  0.021972656  

/* Motor Drivers
 * ----------------------------------------------
 */
/* Id */
#define CAN_ID_DRIVERS        0x30
/* Output range is -10V ... +10V 
                   -32767 ... 32767
 * Scale factor is 3276.7; 
 */ 
#define CAN_AZ_DRIVER_SCALE   3276.7
#define CAN_EL_DRIVER_SCALE   3276.7

#define CAN_AZ_DRIVE_MAX_FLT  10.0
#define CAN_EL_DRIVE_MAX_FLT  10.0
#include <canlib.h>
#include <canlib.h>
 
#define CAN_AZ_DRIVE_MAX_INT  32767
#define CAN_EL_DRIVE_MAX_INT  32767


/* Heart Beat
 * ----------------------------------------------
 */
/* Id */
#define CAN_ID_HEARTBEAT_DRIVER   0x31
#define CAN_ID_HEARTBEAT_CONTROL  0x52

/* ms */
#define CAN_HEARTBEAT_DRIVER_INTERVAL    10 
#define CAN_HEARTBEAT_CONTROL_INTERVAL   500 


/* Typedefs
 * ------------------------------------------------------------------------------
 */
typedef struct
{
  long Id;
  unsigned int MsgSize;
  unsigned char Msg[8];

} CAN_MSG;

/* Function protypes
 * ------------------------------------------------------------------------------
 */
/* ----------------  can_kvaser.c ------------ - */

int CanInit( int iNumOfChan, int *pCanHdl_0, int *pCanHdl_1,
      long lChanSpeed0, long lChanSpeed1 );

int CanStart( int iChannel, int iCanHdl_0, int iCanHdl_1 );

int CanReceive( int iChannel, int iCanHdl_0, int iCanHdl_1, 
      CAN_MSG *pMsg0, CAN_MSG *pMsg1 ); 

int CanSend( int iChannel, int iCanHdl_0, int iCanHdl_1, CAN_MSG *pMsg ); 

int CanStop( int iChannel, int iCanHdl_0, int iCanHdl_1 ); 

int CanClose( int iChannel, int iCanHdl_0, int iCanHdl_1 );


#endif /* _CANBUS_H_ */

