#ifndef MOT_PRIV_H
#define MOT_PRIV_H
#include "hal.h"
#include "../motion/motion.h"
typedef struct {
hal_bit_t *spindle_on;
hal_bit_t *spindle_forward;
hal_bit_t *spindle_reverse;
hal_bit_t *spindle_incr_speed;
hal_bit_t *spindle_decr_speed;
hal_bit_t *spindle_brake;
hal_float_t *spindle_speed_out;
hal_float_t *spindle_speed_out_rps;
hal_float_t *spindle_speed_out_abs;
hal_float_t *spindle_speed_out_rps_abs;
hal_float_t *spindle_speed_cmd_rps;
hal_float_t *spindle_speed_in;
hal_bit_t *spindle_index_enable;
hal_bit_t *spindle_inhibit;
hal_float_t *spindle_revs;
hal_bit_t *spindle_is_atspeed;
hal_bit_t *spindle_amp_fault;
hal_float_t *spindle_orient_angle;
hal_s32_t *spindle_orient_mode;
hal_bit_t *spindle_orient;
hal_bit_t *spindle_locked;
hal_bit_t *spindle_is_oriented;
hal_s32_t *spindle_orient_fault;
} spindle_hal_t;
typedef struct {
hal_float_t *coarse_pos_cmd;
hal_float_t *joint_vel_cmd;
hal_float_t *joint_acc_cmd;
hal_float_t *backlash_corr;
hal_float_t *backlash_filt;
hal_float_t *backlash_vel;
hal_float_t *motor_offset;
hal_float_t *motor_pos_cmd;
hal_float_t *motor_pos_fb;
hal_float_t *joint_pos_cmd;
hal_float_t *joint_pos_fb;
hal_float_t *f_error;
hal_float_t *f_error_lim;
hal_float_t *free_pos_cmd;
hal_float_t *free_vel_lim;
hal_bit_t *free_tp_enable;
hal_bit_t *kb_jjog_active;
hal_bit_t *wheel_jjog_active;
hal_bit_t *active;
hal_bit_t *in_position;
hal_bit_t *error;
hal_bit_t *phl;
hal_bit_t *nhl;
hal_bit_t *f_errored;
hal_bit_t *faulted;
hal_bit_t *pos_lim_sw;
hal_bit_t *neg_lim_sw;
hal_bit_t *amp_fault;
hal_bit_t *amp_enable;
hal_bit_t *unlock;
hal_bit_t *is_unlocked;
hal_s32_t *jjog_counts;
hal_bit_t *jjog_enable;
hal_float_t *jjog_scale;
hal_float_t *jjog_accel_fraction;
hal_bit_t *jjog_vel_mode;
} joint_hal_t;
typedef struct {
hal_float_t *posthome_cmd; } extrajoint_hal_t;
typedef struct {
hal_float_t *pos_cmd;
hal_float_t *teleop_vel_cmd;
hal_float_t *teleop_pos_cmd;
hal_float_t *teleop_vel_lim;
hal_bit_t *teleop_tp_enable;
hal_s32_t *ajog_counts;
hal_bit_t *ajog_enable;
hal_float_t *ajog_scale;
hal_float_t *ajog_accel_fraction;
hal_bit_t *ajog_vel_mode;
hal_bit_t *kb_ajog_active;
hal_bit_t *wheel_ajog_active;
hal_bit_t *eoffset_enable;
hal_bit_t *eoffset_clear;
hal_s32_t *eoffset_counts;
hal_float_t *eoffset_scale;
hal_float_t *external_offset;
hal_float_t *external_offset_requested;
} axis_hal_t;
typedef struct {
hal_bit_t *probe_input;
hal_bit_t *enable;
hal_float_t *adaptive_feed;
hal_bit_t *feed_hold;
hal_bit_t *feed_inhibit;
hal_bit_t *homing_inhibit;
hal_bit_t *tp_reverse;
hal_bit_t *motion_enabled;
hal_bit_t *in_position;
hal_bit_t *coord_mode;
hal_bit_t *teleop_mode;
hal_bit_t *coord_error;
hal_bit_t *on_soft_limit;
hal_s32_t *program_line;
hal_s32_t *motion_type;
hal_float_t *current_vel;
hal_float_t *requested_vel;
hal_float_t *distance_to_go;
hal_bit_t debug_bit_0;
hal_bit_t debug_bit_1;
hal_float_t debug_float_0;
hal_float_t debug_float_1;
hal_float_t debug_float_2;
hal_float_t debug_float_3;
hal_s32_t debug_s32_0;
hal_s32_t debug_s32_1;
hal_bit_t *synch_do[EMCMOT_MAX_DIO];
hal_bit_t *synch_di[EMCMOT_MAX_DIO];
hal_float_t *analog_input[EMCMOT_MAX_AIO];
hal_float_t *analog_output[EMCMOT_MAX_AIO];
hal_float_t traj_pos_out;
hal_float_t traj_vel_out;
hal_u32_t traj_active_tc;
hal_float_t tc_pos[4];
hal_float_t tc_vel[4];
hal_float_t tc_acc[4];
hal_u32_t *last_period;
hal_float_t *last_period_ns;
hal_float_t *tooloffset_x;
hal_float_t *tooloffset_y;
hal_float_t *tooloffset_z;
hal_float_t *tooloffset_a;
hal_float_t *tooloffset_b;
hal_float_t *tooloffset_c;
hal_float_t *tooloffset_u;
hal_float_t *tooloffset_v;
hal_float_t *tooloffset_w;
spindle_hal_t spindle[EMCMOT_MAX_SPINDLES];
joint_hal_t joint[EMCMOT_MAX_JOINTS];
extrajoint_hal_t ejoint[EMCMOT_MAX_EXTRAJOINTS];
axis_hal_t axis[EMCMOT_MAX_AXIS];
hal_bit_t *eoffset_active;
hal_bit_t *eoffset_limited;
} emcmot_hal_data_t;
extern emcmot_hal_data_t *emcmot_hal_data;
extern emcmot_joint_t *joints;
extern emcmot_axis_t *axes;
extern KINEMATICS_FORWARD_FLAGS fflags;
extern KINEMATICS_INVERSE_FLAGS iflags;
extern double servo_freq;
extern struct emcmot_struct_t *emcmotStruct;
extern struct emcmot_command_t *emcmotCommand;
extern struct emcmot_status_t *emcmotStatus;
extern struct emcmot_config_t *emcmotConfig;
extern struct emcmot_debug_t *emcmotDebug;
extern struct emcmot_error_t *emcmotError;
#define ALL_JOINTS emcmotConfig->numJoints
#define NO_OF_KINS_JOINTS (ALL_JOINTS - emcmotConfig->numExtraJoints)
#define IS_EXTRA_JOINT(jno) (jno >= NO_OF_KINS_JOINTS)
extern void emcmotCommandHandler(void *arg, long period);
extern void emcmotController(void *arg, long period);
extern void emcmotSetCycleTime(unsigned long nsec);
extern void emcmotDioWrite(int index, char value);
extern void emcmotAioWrite(int index, double value);
extern void emcmotSetRotaryUnlock(int axis, int unlock);
extern int emcmotGetRotaryIsUnlocked(int axis);
void switch_to_teleop_mode(void);
extern bool checkAllHomed(void);
extern void refresh_jog_limits(emcmot_joint_t *joint,int joint_num);
extern void clearHomes(int joint_num);
extern void emcmot_config_change(void);
extern void reportError(const char *fmt, ...) __attribute((format(printf,1,2)));
int joint_is_lockable(int joint_num);
#define etime() (((double) rtapi_get_time()) / 1.0e9)
#define GET_MOTION_ERROR_FLAG() (emcmotStatus->motionFlag & EMCMOT_MOTION_ERROR_BIT ? 1 : 0)
#define SET_MOTION_ERROR_FLAG(fl) if (fl) emcmotStatus->motionFlag |= EMCMOT_MOTION_ERROR_BIT; else emcmotStatus->motionFlag &= ~EMCMOT_MOTION_ERROR_BIT;
#define GET_MOTION_COORD_FLAG() (emcmotStatus->motionFlag & EMCMOT_MOTION_COORD_BIT ? 1 : 0)
#define SET_MOTION_COORD_FLAG(fl) if (fl) emcmotStatus->motionFlag |= EMCMOT_MOTION_COORD_BIT; else emcmotStatus->motionFlag &= ~EMCMOT_MOTION_COORD_BIT;
#define GET_MOTION_TELEOP_FLAG() (emcmotStatus->motionFlag & EMCMOT_MOTION_TELEOP_BIT ? 1 : 0)
#define SET_MOTION_TELEOP_FLAG(fl) if (fl) emcmotStatus->motionFlag |= EMCMOT_MOTION_TELEOP_BIT; else emcmotStatus->motionFlag &= ~EMCMOT_MOTION_TELEOP_BIT;
#define GET_MOTION_INPOS_FLAG() (emcmotStatus->motionFlag & EMCMOT_MOTION_INPOS_BIT ? 1 : 0)
#define SET_MOTION_INPOS_FLAG(fl) if (fl) emcmotStatus->motionFlag |= EMCMOT_MOTION_INPOS_BIT; else emcmotStatus->motionFlag &= ~EMCMOT_MOTION_INPOS_BIT;
#define GET_MOTION_ENABLE_FLAG() (emcmotStatus->motionFlag & EMCMOT_MOTION_ENABLE_BIT ? 1 : 0)
#define SET_MOTION_ENABLE_FLAG(fl) if (fl) emcmotStatus->motionFlag |= EMCMOT_MOTION_ENABLE_BIT; else emcmotStatus->motionFlag &= ~EMCMOT_MOTION_ENABLE_BIT;
#define GET_JOINT_ENABLE_FLAG(joint) ((joint)->flag & EMCMOT_JOINT_ENABLE_BIT ? 1 : 0)
#define SET_JOINT_ENABLE_FLAG(joint,fl) if (fl) (joint)->flag |= EMCMOT_JOINT_ENABLE_BIT; else (joint)->flag &= ~EMCMOT_JOINT_ENABLE_BIT;
#define GET_JOINT_ACTIVE_FLAG(joint) ((joint)->flag & EMCMOT_JOINT_ACTIVE_BIT ? 1 : 0)
#define SET_JOINT_ACTIVE_FLAG(joint,fl) if (fl) (joint)->flag |= EMCMOT_JOINT_ACTIVE_BIT; else (joint)->flag &= ~EMCMOT_JOINT_ACTIVE_BIT;
#define GET_JOINT_INPOS_FLAG(joint) ((joint)->flag & EMCMOT_JOINT_INPOS_BIT ? 1 : 0)
#define SET_JOINT_INPOS_FLAG(joint,fl) if (fl) (joint)->flag |= EMCMOT_JOINT_INPOS_BIT; else (joint)->flag &= ~EMCMOT_JOINT_INPOS_BIT;
#define GET_JOINT_ERROR_FLAG(joint) ((joint)->flag & EMCMOT_JOINT_ERROR_BIT ? 1 : 0)
#define SET_JOINT_ERROR_FLAG(joint,fl) if (fl) (joint)->flag |= EMCMOT_JOINT_ERROR_BIT; else (joint)->flag &= ~EMCMOT_JOINT_ERROR_BIT;
#define GET_JOINT_PHL_FLAG(joint) ((joint)->flag & EMCMOT_JOINT_MAX_HARD_LIMIT_BIT ? 1 : 0)
#define SET_JOINT_PHL_FLAG(joint,fl) if (fl) (joint)->flag |= EMCMOT_JOINT_MAX_HARD_LIMIT_BIT; else (joint)->flag &= ~EMCMOT_JOINT_MAX_HARD_LIMIT_BIT;
#define GET_JOINT_NHL_FLAG(joint) ((joint)->flag & EMCMOT_JOINT_MIN_HARD_LIMIT_BIT ? 1 : 0)
#define SET_JOINT_NHL_FLAG(joint,fl) if (fl) (joint)->flag |= EMCMOT_JOINT_MIN_HARD_LIMIT_BIT; else (joint)->flag &= ~EMCMOT_JOINT_MIN_HARD_LIMIT_BIT;
#define GET_JOINT_FERROR_FLAG(joint) ((joint)->flag & EMCMOT_JOINT_FERROR_BIT ? 1 : 0)
#define SET_JOINT_FERROR_FLAG(joint,fl) if (fl) (joint)->flag |= EMCMOT_JOINT_FERROR_BIT; else (joint)->flag &= ~EMCMOT_JOINT_FERROR_BIT;
#define GET_JOINT_FAULT_FLAG(joint) ((joint)->flag & EMCMOT_JOINT_FAULT_BIT ? 1 : 0)
#define SET_JOINT_FAULT_FLAG(joint,fl) if (fl) (joint)->flag |= EMCMOT_JOINT_FAULT_BIT; else (joint)->flag &= ~EMCMOT_JOINT_FAULT_BIT;
#if defined(__KERNEL__)
#define HAVE_CPU_KHZ
#endif
#define EOFFSET_EPSILON 1e-8
#endif