int16_t main(void) { init_clock(); init_timer(); init_ui(); init_pin(); init_spi(); init_oc(); init_md(); // Current measurement pin pin_analogIn(&A[0]); // SPI pin setup ENC_MISO = &D[1]; ENC_MOSI = &D[0]; ENC_SCK = &D[2]; ENC_NCS = &D[3]; pin_digitalOut(ENC_NCS); pin_set(ENC_NCS); // Open SPI in mode 1 spi_open(&spi1, ENC_MISO, ENC_MOSI, ENC_SCK, 2e6, 1); // Motor setup md_velocity(&md1, 0, 0); // Get initial angle offset uint8_t unset = 1; while (unset) { ANG_OFFSET = enc_readReg((WORD) REG_ANG_ADDR); unset = parity(ANG_OFFSET.w); } ANG_OFFSET.w &= ENC_MASK; // USB setup InitUSB(); while (USB_USWSTAT!=CONFIG_STATE) { ServiceUSB(); } // Timers timer_setFreq(&timer2, READ_FREQ); timer_setFreq(&timer3, CTRL_FREQ); timer_start(&timer2); timer_start(&timer3); // Main loop while (1) { ServiceUSB(); if (timer_flag(&timer2)) { timer_lower(&timer2); get_readings(); } if (timer_flag(&timer3)) { timer_lower(&timer3); set_velocity(); } } }
int16_t main(void) { init_clock(); init_timer(); init_pin(); init_oc(); init_ui(); InitUSB(); // initialize the USB registers and serial interface engine while (USB_USWSTAT!=CONFIG_STATE) { // while the peripheral is not configured... ServiceUSB(); // ...service USB requests } // Configure Interrupts on the pic IEC1bits.CNIE = 1; CNEN1bits.CN2IE = 1; IFS1bits.CNIF = 0; IEC0bits.OC1IE = 1; IFS0bits.OC1IF = 0; timer_enableInterrupt(&timer1); timer_lower(&timer1); timer_enableInterrupt(&timer2); timer_lower(&timer2); timer_enableInterrupt(&timer4); timer_lower(&timer4); timer_enableInterrupt(&timer5); timer_lower(&timer5); // Configure Pins inPin0 = &A[0]; pin_analogIn(inPin0); inPin1 = &A[1]; pin_analogIn(inPin1); inPin2 = &A[2]; pin_analogIn(inPin2); inPin3 = &A[3]; pin_analogIn(inPin3); inPin4 = &A[4]; pin_analogIn(inPin4); irPin = &A[5]; pin_analogIn(irPin); outPin = &D[6]; pin_digitalOut(outPin); oc_pwm(&oc1, outPin, NULL, 10, (uint16_t)(0)); // write to D2 with a 10Hz PWM signal pin_write(outPin, 10000); //duty doesn't matter, really. redPin = &D[7]; pin_digitalOut(redPin); oc_pwm(&oc2, redPin, NULL, 100, (uint16_t)(0)); greenPin = &D[10]; pin_digitalOut(greenPin); oc_pwm(&oc3, greenPin, NULL, 100, (uint16_t)(0)); bluePin = &D[8]; pin_digitalOut(bluePin); oc_pwm(&oc4, bluePin, NULL, 100, (uint16_t)(0)); pingPin = &D[4]; pin_digitalOut(pingPin); oc_pwm(&oc5, pingPin, &timer3, 40000, 0); receivePin = &D[12]; pin_digitalIn(receivePin); // Motor controller pins dirPin = &D[0]; pin_digitalOut(dirPin); nSleepPin = &D[3]; pin_digitalOut(nSleepPin); pin_write(nSleepPin, 1); stepPin = &D[2]; pin_digitalOut(stepPin); testPin = &D[13]; pin_digitalOut(testPin); timer_setFreq(&timer1, 100); while (1) { ServiceUSB(); // service any pending USB requests irVoltage = pin_read(irPin); if (irVoltage < 40000){ dist = 32768; } if (irVoltage >= 40000){ dist = 32900; } if (dist != stepCount) { changeFlag += 1; } else { changeFlag = 0; } if (changeFlag >= 3){ changeFlag = 0; motorControl(dist); } if (touching0 == 10){ greenTarget = 40000; redTarget = 60000; blueTarget = 0; if (currentPetal == 0){ greenTarget = 0; redTarget = 0; blueTarget = 0; } currentPetal == 0; } if (touching1 == 11){ greenTarget = 20000; redTarget = 20000; blueTarget = 20000; if (currentPetal == 1){ greenTarget = 0; redTarget = 0; blueTarget = 0; } currentPetal == 1; } if (touching2 == 12){ greenTarget = 0; redTarget = 60000; blueTarget = 40000; if (currentPetal == 2){ greenTarget = 0; redTarget = 0; blueTarget = 0; } currentPetal == 2; } if (touching3 == 13){ greenTarget = 0; redTarget = 60000; blueTarget = 0; if (currentPetal == 3){ greenTarget = 0; redTarget = 0; blueTarget = 0; } currentPetal == 3; } if (touching4 == 14){ greenTarget = 60000; redTarget = 0; blueTarget = 0; if (currentPetal == 4){ greenTarget = 0; redTarget = 0; blueTarget = 0; } currentPetal == 4; } if (greenDuty < greenTarget) { greenChange = 1; } else if (greenDuty > greenTarget) { greenChange = -1; } else { greenChange = 0; onTarget += 1; } if (redDuty < redTarget) { redChange = 1; } else if (redDuty > redTarget) { redChange = -1; } else { redChange = 0; onTarget += 1; } if (blueDuty < blueTarget) { blueChange = 1; } else if (blueDuty > blueTarget) { blueChange = -1; } else { blueChange = 0; onTarget += 1; } greenDuty += greenChange; redDuty += redChange; blueDuty += blueChange; pin_write(greenPin, greenDuty); pin_write(redPin, redDuty); pin_write(bluePin, blueDuty); /* // fade on when touched if (touching0 == 10){ if (greenOn == 0){ greenChange = 1; } if (greenOn == 1){ greenChange = -1; } redChange = -1; blueChange = -1; } if (touching1 == 11){ if (redOn == 1){ redChange = -1; } if (redOn == 0){ redChange = 1; } blueChange = -1; greenChange = -1; } if (touching2 == 12){ if (blueOn == 1){ blueChange = -1; } if (blueOn == 0){ blueChange = 1; } greenChange = -1; redChange = -1; } greenDuty = greenDuty + greenChange; if (greenDuty == MAX_INT){ greenDuty = MAX_INT -1; greenOn = 1; greenChange = 0; } if (greenDuty == 0){ greenDuty = 1; greenOn = 0; greenChange = 0; } redDuty = redDuty + redChange; if (redDuty == MAX_INT){ redDuty = MAX_INT - 1; redOn = 1; redChange = 0; } if (redDuty == 0){ redDuty = 1; redOn = 0; redChange = 0; } blueDuty = blueDuty + blueChange; if (blueDuty == MAX_INT){ blueDuty = MAX_INT - 1; blueOn = 1; blueChange = 0; } if (blueDuty == 0){ blueDuty = 1; blueOn = 0; blueChange = 0; } pin_write(greenPin, greenDuty); pin_write(redPin, redDuty); pin_write(bluePin, blueDuty); */ /* if (iteration > 10000) { ping(); iteration = 0; } iteration += 1; */ } }