int main (void) { unsigned char kommando[32]; // Kommandozeilenbuffer unsigned char* k_sp = kommando; // und dazugehöriger Pointer volatile struct buffer put_buffer, get_buffer; // Sende- u. Empfangsbuffer buffer_init(put_buffer); buffer_init(get_buffer); put_bp=&put_buffer; get_bp=&get_buffer; DDRD = 0xE0; // Port D, Pin 6,7,8 als Ausgang tim0_init(); // Initialisiere Timer_0 tim1_init(); // Initialisiere Timer_1 tim2_init(); // Initialisiere Timer_2 uart_init(); // Initialisiere serielle Schnittstelle adc_init(); // Initialisiere Analog-Digitalwandler ADC uart_puts(version); // Startmeldung/Version ausgeben beep(1000); uart_puts("\n> "); // newline und prompt UCSRB |= (1<<RXCIE); // Empfaenger-Interrupts freigeben sei(); // Interrupts global freigeben while(1) { while(buffer_size(get_bp)){ *k_sp = buffer_read(get_bp); uart_putc(*k_sp); if(*k_sp == '\n' || *k_sp == '\r'){ *k_sp = 0x00; k_sp=kommando; uart_putc('\n'); // uart_puts(k_sp); // Ausgabe für Testwecke parser(k_sp); } else k_sp++; // hier noch Bufferoverflow abfangen } if(status_LED2 == 's'){ PORTD ^= 1<<PD6; // toggle LED2 my_delay_ms(delay); // warte delay ms PORTD ^= 1<<PD6; // toggle LED2 status_LED2 = '0'; // Status_Flag auf Aus } if(status_LED2 == 't'){ my_delay_ms(delay); // warte delay ms PORTD ^= 1<<PD6; // toggle LED2 } } /* wird nie erreicht */ return 0; }
int main(void) { RCC_Configuration(); GPIO_Configuration(); RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000 - 1); setup_adc(); tim1_init(); usart_init(); PWM_U = 0; PWM_V = 0; PWM_W = 0; while(1){ if(uartsend == 1){ amp = AMP(amp_raw); volt = VOLT(volt_raw); if(temp_raw < ARES && temp_raw > 0){ temp = TEMP(temp_raw); } from_hv.dc_volt = TOFIXED(volt); from_hv.dc_cur = TOFIXED(amp); from_hv.hv_temp = TOFIXED(temp); #ifdef TROLLER from_hv.dc_cur = TOFIXED(0); from_hv.hv_temp = TOFIXED(0); from_hv.a = TOFIXED(AMP(ADCConvertedValue[1])); from_hv.b = TOFIXED(AMP(ADCConvertedValue[2])); from_hv.c = TOFIXED(AMP(ADCConvertedValue[3])); #endif uartsend = 0; while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); USART_SendData(USART2, 0x154); for(int j = 0;j<sizeof(from_hv_t);j++){ while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); USART_SendData(USART2, ((uint8_t*)&from_hv)[j]); } } //GPIOA->BSRR = (GPIOA->ODR ^ GPIO_Pin_2) | (GPIO_Pin_2 << 16);//toggle red led } }
int main ( void ) { //TRISA = 0b0010000000000000; TRISA = 0b00001111; TRISAbits.TRISA12 = 0; TRISCbits.TRISC1 = 0; TRISCbits.TRISC2 = 0; TRISCbits.TRISC3 = 0; TRISCbits.TRISC4 = 0; //TRISD = 0; TRISD = 0b0000000000010000; // RD11: DIR2; RD8: PWM2 //TRISE = 0b00001000; TRISE = 0b00001000; // RE6: DIR1; RE0: PWM1 TRISEbits.TRISE7 = 0; //LCD an _PCFG16 = 1; // AN16 is digital _PCFG17 = 1; // AN17 is digital _PCFG18 = 1; // AN18 is digital _PCFG19 = 1; // AN19 is digital _PCFG20 =1; _PCFG31=1; //pwm analog select _PCFG24=1; _PCFG30=1; //RE4 for lcd r/s _PCFG28=1; TRISEbits.TRISE4 = 0; AD1PCFGHbits.PCFG28 = 1; AD1PCFGHbits.PCFG27 = 1; AD1PCFGHbits.PCFG30 = 1; AD1PCFGHbits.PCFG24 = 1; AD1PCFGH = 0x0020; TRISAbits.TRISA13 = 1; //set pwm low PWM1=0; PWM2=0; SYS_Initialize ( ) ; CLKDIVbits.FRCDIV = 0; CLKDIVbits.PLLPOST = 0; // N2 = 2 CLKDIVbits.PLLPRE = 0; // N1 = 2 PLLFBD = (Fosc*N1_default*N2_default/Ffrc) - M_default; // M = 8 -> Fosc = 7.3728 MHz * 8 / 2 / 2 = 16 MHz while(!OSCCONbits.LOCK); // Wait for PLL to lock RCONbits.SWDTEN = 0; // Disable Watch Dog Timer //TRISF = 0; gpsLock = 0; lcd_init(); print_lcd("Initializing"); //delay_ms(500); //lcd_clear(); /*lcd test char line1[] = " On Route "; char line2[] = " Arrived "; //send_command_byte(0xFF); //while(1){send_command_byte(0xFF);send_data_byte(0);} // delay_ms(2); //send_command_byte(0x02); // Go to start of line 1 //send_command_byte(0b00001111); // Display: display on, cursor on, blink on //send_command_byte(0b00000110); // Set entry mode: ID=1, S=0 //send_command_byte(0b00000001); // Clear display print_lcd(line1); delay_ms(5000); lcd_clear(); print_lcd(line2); //send_command_byte(0xC0); // Go to start of line 1 //while(1){send_command_byte(0b00000001);} while(1);*/ /*int a; long long int ct; int i; int j=0;*/ //i2c init //uart init UART_Initialize(); delayMs(100); /* while(1){//test for delay ms configuration //PORTDbits.RD1 = 1; //delayUs(10); //for(i = 0;i <7;i++); delayMs(10); PORTDbits.RD12 = 1; //for (i = 0; i < 1000; i++); //PORTDbits.RD1 = 0; //for(i = 0;i <7;i++); delayMs(10); PORTDbits.RD12 = 0; //for (i = 0; i < 1000; i++); }*/ //ultrasonic test /* while(1){ long double x; delayMs(500); PORTEbits.RE4 = 1; //TRIGGER HIGH PORTDbits.RD5 = 1; delay_us(10); //10uS Delay lcd_clear(); ultraSonicEn = 1; ultraSonicCount = 0; PORTEbits.RE4 = 0; //TRIGGER LOW PORTDbits.RD5 = 0; while (!PORTDbits.RD4); //Waiting for Echo IEC0bits.T2IE = 1; //enable timer while(PORTDbits.RD4);// IEC0bits.T2IE = 0; //disable timer x = ultraSonicCount/TICKS_PER_METER; sprintf(outputBuf,"%lf",x); print_lcd(outputBuf); }*/ //TX_str(endGPS); //delayMs(3000); //TX_str(startGPS); /*TX_str(startGPShot); delayMs(2000); while ( !gpsLock ) { TX_str(getGPS); delayMs(500); }*/ //TCP code TX_str(openNet); delayMs(5000); TX_str(openConnection); delayMs(5000); // while(!BUTTON_IsPressed ( BUTTON_S3 )); //TX_str(sprintf("%s%d\r",sendTCP, strlen("10"))); sprintf(outputBuf2,"2\n%lf,%lf\n\r",roverLog,roverLat ); sprintf(cmdBuf,"%s%d\r", sendTCP,strlen(outputBuf2)); TX_str(cmdBuf); delayMs(3000); TX_str(outputBuf2); while(!waypointsReady || !gpsLock); //while(1); delayMs(7000); lcd_clear(); print_lcd("waypoints locked"); i2c_init(); //i2c_write3(0x3c, 0x00, 0x70); i2c_write3(0x3c, 0x00, 0b00011000); //i2c_write3(0x3c, 0x01, 0b11000000); i2c_write3(0x3c, 0x01, 0xA0); i2c_write3(0x3c, 0x02, 0x00); //timer init, do this after other initializations motorDuty=0; tim1_init(); tim2_init(); /* double angleTolerance = 8.0; motorDuty=2; while (1){ //adjust double angleDiff = headingDiff(0,roverHeading ); if (angleDiff > 0 ||abs(angleDiff) > 175 ){ //turn left PWM1_DIR = 0; //left NOTE: 0 is forward, 1 is reverse PWM2_DIR = 1; //right } else{ // turn right PWM1_DIR = 1; PWM2_DIR = 0; } if (abs(angleDiff) < angleTolerance){ motorDuty = 0; //break; }else{ //motorDuty =4; motorDuty =2; } delayMs(13); updateHeading(); }*/ //hardcoded gps for tcpip test /*while(1){ // PORTDbits.RD5 = 1; // for (a = 0; a < (100/33); a++) {} // PORTDbits.RD5 = 0; // for (a = 0; a < (100/33); a++) {} ct = 0; //TMR2 = 0;//Timer Starts delayMs(60); PORTEbits.RE4 = 1; //TRIGGER HIGH PORTDbits.RD5 = 1; delay_us(15); //10uS Delay PORTEbits.RE4 = 0; //TRIGGER LOW PORTDbits.RD5 = 0; while (!PORTDbits.RD4){ //Waiting for Echo ct++; } //T2CONbits.TON = 1; while(PORTDbits.RD4) { ct++; }// { //T2CONbits.TON = 0; sprintf(outputBuf,"%lld", ct); //a = TMR2; //a = a / 58.82; //long int p; //for(p = 0; p <100000; p++); }*/ //tim1_init(); //while ( 1 ); //UART_Initialize(); /* Infinite Loop */ /* long int i; while ( 1 ) {//test for delay ms configuration //PORTDbits.RD1 = 1; delayMs(10); PORTDbits.RD12 = 1; //for (i = 0; i < 1000; i++); //PORTDbits.RD1 = 0; delayMs(10); PORTDbits.RD12 = 0; //for (i = 0; i < 1000; i++); }*/ //IEC0bits.T1IE = 0; //IEC0bits.T1IE = 1; //motorDuty =2; motorStopFlag = 0; //ready to go char tempBuf1[50]; int wapointsVisited; double desiredHeading = 0; double dToWaypoint = 99999.9; double angleTolerance = 6.0; for (wapointsVisited =0; wapointsVisited<numGPSpoints;wapointsVisited++ ) { dToWaypoint = 99999.9; while (1) { int f; roverLog = 0; roverLat = 0; for(f = 0; f < ROVER_LEN; f++){ roverLog+=roverlog[f]/ROVER_LEN_D; roverLat+=roverlat[f]/ROVER_LEN_D; } dToWaypoint = dist(convertGPSToDeg(roverLat),convertGPSToDeg(roverLog),convertGPSToDeg(gpsLat[wapointsVisited]),convertGPSToDeg(gpsLonge[wapointsVisited])); if(dToWaypoint <= 3.0){break;} //go to next waypoint desiredHeading = 330.0; desiredHeading = bearing(convertGPSToDeg(roverLat),convertGPSToDeg(roverLog),convertGPSToDeg(gpsLat[wapointsVisited]),convertGPSToDeg(gpsLonge[wapointsVisited])); updateHeading(); if (!(abs(headingDiff(desiredHeading,roverHeading )) < angleTolerance)) { motorDuty = 0; //stop delayMs(300); while (1) //ADJUSTMENT LOOP { double angleDiff = headingDiff(desiredHeading,roverHeading ); //double dist = ultraSonicPing(); /*sprintf(tempBuf1,"%f",dist); lcd_clear(); print_lcd(tempBuf1);*/ if (abs(angleDiff) < angleTolerance){ motorDuty = 0; /*sprintf(tempBuf1,"%f",roverHeading); IEC0bits.T1IE = 0; print_lcd(tempBuf1); IEC0bits.T1IE = 1;*/ break; } if (angleDiff > 0 || abs(angleDiff) > 175 ){ //turn right /*PORTDbits.RD4 = 1; //right 0 is forwards, 1 i backwards PORTDbits.RD2 = 1; PORTDbits.RD8 = 0; //left*/ PWM1_DIR = 0; //left NOTE: 0 is forward, 1 is reverse PWM2_DIR = 1; //right } else{ // turn left /*PORTDbits.RD4 = 0; //right 0 is forwards, 1 i backwards PORTDbits.RD2 = 0; PORTDbits.RD8 = 1; //left*/ PWM1_DIR = 1; PWM2_DIR = 0; } motorDuty =3; delayMs(15); updateHeading(); // sprintf(tempBuf1,"%f",roverHeading); // IEC0bits.T1IE = 0; // lcd_clear(); // print_lcd(tempBuf1); // IEC0bits.T1IE = 1; //delayMs(1000); //for(p = 0; p <100000; p++); } } PWM1_DIR = 0; //left NOTE: 0 is forward, 1 is reverse PWM2_DIR = 0; motorDuty = 5; ultraSonicDelayEnable = 1; //frequency is around 20kHz while (ultraSonicDelayCount < 60000){ //for 4 seconds poll ultrasonic and check for obsticles long double x; //PORTEbits.RE4 = 1; //TRIGGER HIGH PORTDbits.RD5 = 1; delay_us(10); //10uS Delay lcd_clear(); ultraSonicEn = 1; ultraSonicCount = 0; //PORTEbits.RE4 = 0; //TRIGGER LOW PORTDbits.RD5 = 0; while (!PORTDbits.RD4); //Waiting for Echo IEC0bits.T2IE = 1; //enable timer while(PORTDbits.RD4);// IEC0bits.T2IE = 0; //disable timer ultraSonicEn = 0; x = ultraSonicCount/TICKS_PER_METER; if (x <= 1.4){ motorDuty = 0; }else{ motorDuty = 5; } delayMs(200); } ultraSonicDelayEnable = 0; ultraSonicDelayCount = 0; } motorDuty = 0; delayMs(1000); //IEC0bits.T1IE = 0; lcd_clear(); char buf3[40]; sprintf(buf3, "reached %d", wapointsVisited); print_lcd(buf3); delayMs(3000); //IEC0bits.T1IE = 1; } //IEC0bits.T1IE = 0; lcd_clear(); print_lcd("ARRIVED!!!"); //IEC0bits.T1IE = 1; while (1); }
int main(void) { InitPeripherals(); mRedOFF; mGreenOFF; mBlueOFF; // startup blink const float kShortDelay = 0.1; const float kLongDelay = 0.3; mRedON; mGreenON; mBlueON; DelaySeconds(kShortDelay); mRedOFF; mGreenOFF; mBlueOFF; DelaySeconds(kLongDelay); mRedON; mGreenON; mBlueON; DelaySeconds(kShortDelay); mRedOFF; mGreenOFF; mBlueOFF; DelaySeconds(kLongDelay); mRedON; mGreenON; mBlueON; DelaySeconds(kShortDelay); mRedOFF; mGreenOFF; mBlueOFF; DelaySeconds(kLongDelay); UsbInterface usb = UsbInterface(); usb.Init(); PersistentMemory mem; DelaySeconds(1.0f); ZigbeeInterface zig = ZigbeeInterface(); DelaySeconds(0.05f); zig.Init(mem); error_reporting_com1 = &usb; error_reporting_com2 = &zig; Mpu60XXImu imu(mem); GyroAccelDriftUkfIo est; BatteryMonitor battery(mem); battery_ptr = &battery; MotorHal motor_hal(mem); motor_hal_ptr = &motor_hal; // Get_pos addition: InitPositionSensor(); tim1_set_position_callback_2(get_position); QuatPD pd(mem); PulsingCoaxControllerQuat uav(mem, pd, imu, est, motor_hal); CoaxOpenController open_controller(mem); CoaxOpenAttitudeController open_attitude_controller(mem, est); monitor = LoopMonitor(mem); UavReporter reporter(mem, imu, est, motor_hal, battery, uav, open_attitude_controller, pd, monitor); StateMachine state_machine; mem.Freeze(); // freeze memory to make writes possible imu.InitFromMemory(); //imu.DefaultAccelSensitivity(1); // overwrite scale for off-datasheet fix imu.flip_z = 1; // flip z-axis direction for upside-down imu #ifdef STREAM_IMU_RAW imu_raw_msg_logger_init(imu); #endif monitor.InitFromMemory(); PlayTimebase(); imu.StartRead(); DelayMilliseconds(10); tim1_init(); tim1_set_supply_volts(3.7f); ////////////////////////////////////////////////////////////////////////////// // Main Loop while(1) { monitor.Profile(0); //////////////////////////////////////////////////////////////////////////// // Get IMU Data and Start New Measurement while(!imu.FinishRead()) {}; #ifdef STREAM_IMU_RAW imu_raw_msg_logger_push(); #endif DelayMicroseconds(50); // this seems to be critical (?!?) imu.StartRead(); monitor.Profile(1); //////////////////////////////////////////////////////////////////////////// // Update Estimator with Measurement est.Update(imu.time, imu.w, imu.a); //////////////////////////////////////////////////////////////////////////// // Control // update control laws uav.Update(); open_controller.Update(); open_attitude_controller.Update(); // map controllers to outputs based on state enum control_state state = state_machine.get_state(); // in STOP state, send active kill messages to motors if(state == kStop) { mGreenOFF; mAmberON; motor_hal.set_top_cmd_volts(0); motor_hal.set_top_cmd_volts_pulse_amp(0); motor_hal.set_top_cmd_pulse_phase(0); motor_hal.set_bottom_cmd_volts(0); } // in STANDBY state, send no motor commands except on entry else if(state == kStandby) { mGreenOFF; mAmberOFF; if(state_machine.get_standby_needs_init()) { motor_hal.set_top_cmd_volts(0); motor_hal.set_top_cmd_volts_pulse_amp(0); motor_hal.set_top_cmd_pulse_phase(0); motor_hal.set_bottom_cmd_volts(0); state_machine.clear_standby_needs_init(); } } // in QUAT state, send motor commands according to quat control law else if(state == kQuat) { mGreenON; mAmberOFF; motor_hal.set_top_cmd_volts(uav.top_mean); motor_hal.set_top_cmd_volts_pulse_amp(uav.top_pulse_amp); motor_hal.set_top_cmd_pulse_phase(uav.top_pulse_phase); motor_hal.set_bottom_cmd_volts(uav.bottom_mean); } // in OPEN state, send motor command according to open motor control commands else if(state == kOpen) { mGreenON; mAmberOFF; motor_hal.set_top_cmd_volts(open_controller.top_mean); motor_hal.set_top_cmd_volts_pulse_amp(open_controller.top_pulse_amp); motor_hal.set_top_cmd_pulse_phase(open_controller.top_pulse_phase); motor_hal.set_bottom_cmd_volts(open_controller.bottom_mean); } // in OPEN ATTITUDE state, send motor command according to open motor control commands else if(state == kOpenAttitude) { mGreenON; mAmberOFF; motor_hal.set_top_cmd_volts(open_attitude_controller.top_mean); motor_hal.set_top_cmd_volts_pulse_amp(open_attitude_controller.top_pulse_amp); motor_hal.set_top_cmd_pulse_phase(open_attitude_controller.top_pulse_phase); motor_hal.set_bottom_cmd_volts(open_attitude_controller.bottom_mean); } monitor.Profile(2); //////////////////////////////////////////////////////////////////////////// // Packet Communication uint8_t is_data; // 1 iff data received uint8_t *rx_data; // temporary pointer to received type+data bytes uint8_t rx_length; // number of received type+data bytes //////////////////////////////////////////////////////////////////////////// // USB Input is_data = 0; usb.GetBytes(); while(usb.PeekPacket(&rx_data, &rx_length)) { zig.ReadMsg(usb, rx_data, rx_length); imu.ReadMsg(usb, rx_data, rx_length); est.ReadMsg(usb, rx_data, rx_length); mem.ReadMsg(usb, rx_data, rx_length); uav.ReadMsg(usb, rx_data, rx_length); open_controller.ReadMsg( usb, rx_data, rx_length); open_attitude_controller.ReadMsg( usb, rx_data, rx_length); pd.ReadMsg( usb, rx_data, rx_length); monitor.ReadMsg( usb, rx_data, rx_length); motor_hal.ReadMsg(usb, rx_data, rx_length); battery.ReadMsg( usb, rx_data, rx_length); reporter.ReadMsg( usb, rx_data, rx_length); state_machine.ReadMsg(usb, rx_data, rx_length); usb.DropPacket(); is_data = 1; } // while peek... if(is_data) { usb.SendNow(); } //////////////////////////////////////////////////////////////////////////// // Radio Input is_data = 0; zig.GetBytes(); while(zig.PeekPacket(&rx_data, &rx_length)) { #ifdef STREAM_IMU_RAW if(rx_data[0] == kTypeQuatPilot || rx_data[0] == kTypeQuatFullObsPilot || rx_data[0] == kTypeOpenPilot || rx_data[0] == kTypeOpenAttitudePilot) { imu_raw_msg_logger_send(zig); } #endif zig.ReadMsg(zig, rx_data, rx_length); imu.ReadMsg(zig, rx_data, rx_length); est.ReadMsg(zig, rx_data, rx_length); mem.ReadMsg(zig, rx_data, rx_length); uav.ReadMsg(zig, rx_data, rx_length); open_controller.ReadMsg( zig, rx_data, rx_length); open_attitude_controller.ReadMsg( zig, rx_data, rx_length); pd.ReadMsg( zig, rx_data, rx_length); monitor.ReadMsg( zig, rx_data, rx_length); motor_hal.ReadMsg(zig, rx_data, rx_length); battery.ReadMsg( zig, rx_data, rx_length); reporter.ReadMsg( zig, rx_data, rx_length); state_machine.ReadMsg(zig, rx_data, rx_length); zig.DropPacket(); is_data = 1; } // while peek... monitor.Profile(3); if(is_data) { zig.SendNow(); } monitor.Profile(4); monitor.Profile(5); //////////////////////////////////////////////////////////////////////////// // Throttle main loop to main_freq monitor.EndMainLoop(); monitor.Profile(6); //monitor.SendProfile(usb); //usb.SendNow(); } // while(1) return(0); }