int16_t main(void) { //initialize all system clocks init_clock(); //initialize serial communications init_uart(); //initialize pin driving library (to be able to use the &D[x] defs) init_pin(); //initialize the UI library init_ui(); //initialize the timer module init_timer(); //initialize the OC module (used by the servo driving code) init_oc(); imu_init() //Set servo control pins as output pin_digitalOut(PAN_PIN); pin_digitalOut(TILT_PIN); pin_digitalOut(SONIC_OUT_PIN); pin_digitalIn(SONIC_IN_PIN); //Set LED off led_off(LED); //Configure blinking rate for LED when connected timer_setPeriod(LED_TIM, 0.2); timer_start(LED_TIM); //Configure timer for reciever timeout timer_setPeriod(DIST_TIM, 0.05); //configure PWM on sonic output pin oc_pwm(PWM_OC, SONIC_OUT_PIN, NULL, SONIC_FREQ, 0x0000); //According to HobbyKing documentation: range .8 through 2.2 msec //Set servo control pins as OC outputs on their respective timers oc_servo(SERVO1_OC, PAN_PIN, SERVO1_TIM, SERVO_PERIOD, SERVO_MIN, SERVO_MAX, pan_set_val); oc_servo(SERVO2_OC, TILT_PIN, SERVO2_TIM, SERVO_PERIOD, SERVO_MIN, SERVO_MAX, tilt_set_val); InitUSB(); // initialize the USB registers and serial interface engine while (USB_USWSTAT!=CONFIG_STATE) { // while the peripheral is not configured... ServiceUSB(); // ...service USB requests led_on(LED); //There's no point in driving the servos when there's no one connected yet. } while (1) { ServiceUSB(); // service any pending USB requests //blink the LED if (timer_flag(LED_TIM)) { timer_lower(LED_TIM); led_toggle(LED); } //Update the servo control values. x_gout = gyro_read(OUT_X_L); } }
int16_t main(void) { init_clock(); init_uart(); init_ui(); init_timer(); init_oc(); led_on(&led2); timer_setPeriod(&timer2, 0.5); timer_start(&timer2); val1 = 0; val2 = 0; interval = 0.02; min_width = 5.5E-4; max_width = 2.3E-3; pos = 0; //16 bit int with binary point in front of the MSB oc_servo(&oc1,&D[0],&timer1, interval,min_width, max_width, pos); oc_servo(&oc2,&D[2],&timer3, interval,min_width, max_width, pos); printf("Good morning\n"); InitUSB(); // initialize the USB registers and serial interface engine while (USB_USWSTAT!=CONFIG_STATE) { // while the peripheral is not configured... ServiceUSB(); // ...service USB requests } while (1) { ServiceUSB(); //write the values to the servos (move the servos to the requested position) pin_write(&D[0],val1); pin_write(&D[2],val2); if (timer_flag(&timer2)) { //show a heartbeat and a status message timer_lower(&timer2); led_toggle(&led1); printf("val1 = %u, val2 = %u\n", val1, val2); } } }
int16_t main(void) { init_pin(); init_clock(); init_uart(); init_ui(); init_timer(); init_oc(); //setup the signal input pin pin_digitalIn(&D[4]); val1 = 0; val2 = 0; pos = 0; //16 bit int with binary point in front of the MSB led_on(&led2); timer_setPeriod(&timer2, PULSE_FREQUENCY); //how often we send a pulse timer_start(&timer2); timer_setPeriod(&timer3, 0.5); //heartbeat timer_start(&timer3); oc_servo(&oc1,&D[0],&timer4, INTERVAL,MIN_WIDTH, MAX_WIDTH, pos); oc_servo(&oc2,&D[2],&timer5, INTERVAL,MIN_WIDTH, MAX_WIDTH, pos); oc_pwm(&oc3,&D[3],NULL,FREQ,ZERO_DUTY); printf("Good morning\n"); InitUSB(); // initialize the USB registers and serial interface engine while (USB_USWSTAT!=CONFIG_STATE) { // while the peripheral is not configured... ServiceUSB(); // ...service USB requests } while (1) { ServiceUSB(); pin_write(&D[0],val1); pin_write(&D[2],val2); //adapted from Patrick and Charlie's approach if (!send_pulse && timer_read(&timer2) < PULSE_WIDTH){ send_pulse = 1; pin_write(&D[3],HALF_DUTY); get_distance = 1; } else if (send_pulse && timer_read(&timer2) >= PULSE_WIDTH) { send_pulse = 0; pin_write(&D[3],ZERO_DUTY); } if (timer_read(&timer2) >= ECHO_TIME) { if (pin_read(&D[4]) && get_distance) { printf("%d\n", timer_read(&timer2)); get_distance = 0; } } if (timer_flag(&timer3)) { //show a heartbeat and a status message timer_lower(&timer3); led_toggle(&led1); } } }