/* vti_counter_init() - Initializes the channel works the same for both cards (vti & STG2) */ static int vti_counter_init(int counters) { int i, retval=0; /* export all the variables for each counter, dac */ for (i = 0; i < counters; i++) { /* export all vars */ retval = export_counter(i, vti_driver); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "VTI: ERROR: counter %d var export failed\n", i + 1); hal_exit(comp_id); return -1; } /* init counter */ *(vti_driver->count[i]) = 0; *(vti_driver->pos[i]) = 0.0; vti_driver->pos_scale[i] = 1.0; } return 0; }
int rtapi_app_main(void) { int n, retval; counter_t *cntr; /* test for number of channels */ if ((num_chan <= 0) || (num_chan > MAX_CHAN)) { rtapi_print_msg(RTAPI_MSG_ERR, "ENCODER: ERROR: invalid num_chan: %d\n", num_chan); return -1; } /* have good config info, connect to the HAL */ comp_id = hal_init("encoder"); if (comp_id < 0) { rtapi_print_msg(RTAPI_MSG_ERR, "ENCODER: ERROR: hal_init() failed\n"); return -1; } /* allocate shared memory for counter data */ counter_array = hal_malloc(num_chan * sizeof(counter_t)); if (counter_array == 0) { rtapi_print_msg(RTAPI_MSG_ERR, "ENCODER: ERROR: hal_malloc() failed\n"); hal_exit(comp_id); return -1; } /* init master timestamp counter */ timebase = 0; /* export all the variables for each counter */ for (n = 0; n < num_chan; n++) { /* point to struct */ cntr = &(counter_array[n]); /* export all vars */ retval = export_counter(n, cntr); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "ENCODER: ERROR: counter %d var export failed\n", n); hal_exit(comp_id); return -1; } /* init counter */ cntr->state = 0; cntr->oldZ = 0; cntr->Zmask = 0; *(cntr->x4_mode) = 1; *(cntr->counter_mode) = 0; *(cntr->latch_rising) = 1; *(cntr->latch_falling) = 1; cntr->buf[0].count_detected = 0; cntr->buf[1].count_detected = 0; cntr->buf[0].index_detected = 0; cntr->buf[1].index_detected = 0; cntr->bp = &(cntr->buf[0]); *(cntr->raw_counts) = 0; cntr->raw_count = 0; cntr->timestamp = 0; cntr->index_count = 0; cntr->latch_count = 0; *(cntr->count) = 0; *(cntr->min_speed) = 1.0; *(cntr->pos) = 0.0; *(cntr->pos_latch) = 0.0; *(cntr->vel) = 0.0; *(cntr->pos_scale) = 1.0; cntr->old_scale = 1.0; cntr->scale = 1.0; cntr->counts_since_timeout = 0; } /* export functions */ retval = hal_export_funct("encoder.update-counters", update, counter_array, 0, 0, comp_id); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "ENCODER: ERROR: count funct export failed\n"); hal_exit(comp_id); return -1; } retval = hal_export_funct("encoder.capture-position", capture, counter_array, 1, 0, comp_id); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "ENCODER: ERROR: capture funct export failed\n"); hal_exit(comp_id); return -1; } rtapi_print_msg(RTAPI_MSG_INFO, "ENCODER: installed %d encoder counters\n", num_chan); hal_ready(comp_id); return 0; }