/** * @brief Programa principal */ int main(void) { // Configure system configSystem(); // Initialize system status estados_t estados; estados = ESTADO_INICIAL; can_t received_msg; //! Armazena a mensagem recebida via CAN // main loop: reads a message and sends it via USART for(;;){ // Checks if a new message was received if (can_check_message()){ // Try to read the message if(can_get_message(&received_msg)){ // Sends via USART sendSystemStatusUSART(received_msg.data[0]); } }else{ usartTransmit(0); } // Updates system status updateSystemStatus(&estados); // Sends system status via CAN sendSystemStatusCAN(estados); _delay_ms(500); } }
void main(void) { //setupOsc(); //RD2 = 1; configSystem(); configSoftPWM(); configTimer(); configI2C(); // configADC(); //adcOff(); while (1) { //**PID Current control** // Read current on channel // 1. Read ADC value // 2. Scale to voltage // 3. V*100=I // Calculate Error between current I(current) and setpoint // 1. Scale setpoint to 0 - Max current // 1. Get Max Current (This needs to be done on init) // 2. Err = Setpoint - Current Current // 3. Err * Pi = Pterm // dutyCycle += Pterm //Operate on received command if(newData){ switch(i2c_rx_buff[0]){ /****** Set Motor 1 Speed ********/ case 0x00: if (i2c_rx_buff[1] > 32) { dutyCycle2 = i2c_rx_buff[1] - 32; dutyCycle1 = 0; } else{ dutyCycle1 = 32 - i2c_rx_buff[1]; dutyCycle2 = 0; } newData = 0; break; /****** Set Motor 2 Speed ********/ case 0x01: if (i2c_rx_buff[1] > 32) { dutyCycle4 = i2c_rx_buff[1] - 32; dutyCycle3 = 0; } else{ dutyCycle3 = 32 - i2c_rx_buff[1]; dutyCycle4 = 0; } newData = 0; break; /****** Read Motor 1 Speed ********/ case 0x10: i2c_tx_buff[0] = dutyCycle1; break; /****** Read Motor 2 Speed ********/ case 0x20: i2c_tx_buff[0] = dutyCycle2; break; /********* Read ADC ***************/ case 100: i2c_tx_buff[0] = readADC(); break; /****** Enable ADC ****************/ case 0x99: potEnabled = i2c_rx_buff[1]; break; default: //RB4 = 1; newData = 0; break; } } //Unsupported command //else RB4 = 1; //dutyCycle1 = i2c_rx_buff[0]; } }
int configurationPage::finishUp() { pageBase::finishUp(); configSystem(); return 0; }