void envoi_CAN(int ID) { switch(ID) { case CAN_MSG_EEPROM_CONFIG_ACK_SID: chinookpack_pack_uint8(&pk,EEPROM_REQUEST); Set_Timeout(); send_CAN_msg(&can_msg_EEPROM_ACK,canBuffer, 2); while(!is_CAN_msg_send(&can_msg_EEPROM_ACK) && !can_time_out); Reset_Timeout(); // If you need to clear the packer buffer chinookpack_fbuffer_clear(&buf); break; case CAN_MSG_GEAR_SID : chinookpack_pack_uint8(&pk,gear); Set_Timeout(); send_CAN_msg(&can_msg_GEAR,canBuffer, 2); while(!is_CAN_msg_send(&can_msg_GEAR) && !can_time_out); Reset_Timeout(); // If you need to clear the packer buffer chinookpack_fbuffer_clear(&buf); break; case CAN_MSG_TURBINE_DIRECTION_SID : chinookpack_pack_float(&pk,Position_mat); Set_Timeout(); send_CAN_msg(&can_msg_POSITION_MAT,canBuffer, 5); while(!is_CAN_msg_send(&can_msg_POSITION_MAT) && !can_time_out); Reset_Timeout(); // If you need to clear the paker buffer chinookpack_fbuffer_clear(&buf); break; default: break; } }
struct RxReturn RxPacket(void) { struct RxReturn respuesta; byte bCount, bLength, bChecksum; byte Rx_time_out = 0; Sentit_Dades_Rx(); init_A1_timer(); for(bCount = 0; bCount < 4; bCount++) { Reset_Timeout(); Byte_Recibido=false; //No_se_ha_recibido_Byte(); while (!Byte_Recibido) { Rx_time_out=TimeOut(5000); // tiempo en decenas de microsegundos if (Rx_time_out)break; } if (Rx_time_out)break; //sale del for si ha habido Timeout respuesta.StatusPacket[bCount] = DatoLeido_UART; } bLength = bCount; bChecksum=0; if (!Rx_time_out) { if(bLength>3) //podem fer checking { if(respuesta.StatusPacket[0]!=0xff || respuesta.StatusPacket[0]!=0xff) { //WRONG HEADER //netejar buffer } if(respuesta.StatusPacket[3]!=bLength-4) { //WRONG LENGTH //NETEJAR BUFFER } for(bCount = 2; bCount<bLength; bCount++) bChecksum += respuesta.StatusPacket[bCount]; if(bChecksum!=0xff) { //WRONG CHECKSUM //NETEJAR BUFFER } } } return respuesta; }
int main(void) { /*Disable watchdog*/ RCONbits.SWDTEN = 0; set_clk(); DataStructInit(&sSensorValues); NotusInitIOs(); NotusInitPeripherals(); NotusInitDevices(); /*CAN bus initialization*/ /*Encoder & decoder*/ chinookpack_fbuffer_init(&buf,bTxCanBuffer,8); chinookpack_packer_init(&pk,&buf,chinookpack_fbuffer_write); chinookpack_unpacked_init(&unpacker); /*Msg*/ /*configuration du message pour la direction de l'eolienne */ config_CAN_filter(0, CAN_MSG_TURBINE_DIRECTION_SID , STANDARD_ID); receive_CAN_msg(0, 3, fct_can_turbine_direction); /*configuration du message pour le gear */ config_CAN_filter(1, CAN_MSG_GEAR_SID , STANDARD_ID); receive_CAN_msg(1, 3, fct_can_gear); /*configuration du message pour l'eeprom ack */ config_CAN_filter(2, CAN_MSG_EEPROM_CONFIG_ACK_SID , STANDARD_ID); receive_CAN_msg(2, 3, fct_can_eeprom_answer); /*configuration du message pour l'eeprom ack */ config_CAN_filter(3, CAN_MSG_PITCH_ACK_CONFIG , STANDARD_ID); receive_CAN_msg(3, 3, fct_can_pitch_eeprom); /*configuration du message pour le pitch */ config_CAN_filter(4, CAN_MSG_MANUAL_PITCH_SID , STANDARD_ID); receive_CAN_msg(4, 3, fct_can_pitch); config_CAN_Tx_msg(&sCanMsgTurbineRPM,CAN_MSG_TURBINE_RPM_SENSOR_SID,STANDARD_ID,3); config_CAN_Tx_msg(&sCanMsgWheelRPM,CAN_MSG_WHEEL_RPM_SID,STANDARD_ID,3); config_CAN_Tx_msg(&sCanMsgWindSpeed,CAN_MSG_WIND_SPEED_SID,STANDARD_ID,3); config_CAN_Tx_msg(&sCanMsgWindDirection,CAN_MSG_WIND_DIRECTION_SID,STANDARD_ID,3); config_CAN_Tx_msg(&sCanMsgTrust,CAN_MSG_TRUST_SID,STANDARD_ID,3); config_CAN_Tx_msg(&sCanMsgAccelerometer,CAN_MSG_ACCELERATION_SID,STANDARD_ID,3); config_CAN_Tx_msg(&sCanMsgEepromConfigSend,CAN_MSG_EEPROM_CONFIG_SEND_SID,STANDARD_ID,3); config_CAN_Tx_msg(&sCanMsgEepromPitchSend,CAN_MSG_PITCH_CONFIG_SEND,STANDARD_ID,3); /*UART command decoder initialization*/ UartSetRXLineEvt(UART_1,uartReceiveLineEvt); skadi_init(&skadi, skadiCommandTable,sizeof(skadiCommandTable)/sizeof(SkadiCommand)); while(TRUE) { /*Get sensors data*/ GetTrust(&sSensorValues); GetWindDirection(&sSensorValues); GetWindSpeed(&sSensorValues); GetTurbineRPM(&sSensorValues); GetWheelRPM(&sSensorValues); Get3V3Sensing(&sSensorValues); Get5V0Sensing(&sSensorValues); //GetsAccelerometer0(&sSensorValues); /*Uart command processing*/ if(uartline_rcv_new){ skadi_process_command(&skadi,uartline_rcv); uartline_rcv_new=0; } if(print) { LED_STAT2^=1; /*Send CAN messages*/ /*Initial message to set turbine direction and gear*/ if(ubEepromAnswerReceive == 0x55) { ubEepromGearInit = ReadGearEeprom(); fEepromTurbineDirectionInit = ReadTurbineDirEeprom(); chinookpack_pack_uint8(&pk,ubEepromGearInit); chinookpack_pack_float(&pk,fEepromTurbineDirectionInit); Set_Timeout(); send_CAN_msg(&sCanMsgEepromConfigSend,bTxCanBuffer, 7); while(!is_CAN_msg_send(&sCanMsgEepromConfigSend) && !sSystemFlags.CanTimeout); Reset_Timeout(); chinookpack_fbuffer_clear(&buf); } /*Initial message to set pitch position*/ if(ubPitchEepromAnswerReceive == 0x55) { fEepromPitchInit = ReadPitchEeprom(); chinookpack_pack_float(&pk,fEepromPitchInit); Set_Timeout(); send_CAN_msg(&sCanMsgEepromPitchSend,bTxCanBuffer, 5); while(!is_CAN_msg_send(&sCanMsgEepromPitchSend) && !sSystemFlags.CanTimeout); Reset_Timeout(); chinookpack_fbuffer_clear(&buf); } chinookpack_pack_float(&pk,sSensorValues.fTurbineRPM); Set_Timeout(); send_CAN_msg(&sCanMsgTurbineRPM,bTxCanBuffer, 5); while(!is_CAN_msg_send(&sCanMsgTurbineRPM) && !sSystemFlags.CanTimeout); Reset_Timeout(); chinookpack_fbuffer_clear(&buf); /* chinookpack_pack_float(&pk,sSensorValues.sAccelerometer0.fGx); Set_Timeout(); send_CAN_msg(&sCanMsgAccelerometer,bTxCanBuffer, 5); while(!is_CAN_msg_send(&sCanMsgAccelerometer) && !sSystemFlags.CanTimeout); Reset_Timeout(); chinookpack_fbuffer_clear(&buf); */ /*EEPROM gear and turbine position*/ if(ubEepromAnswerReceiveMem != ubGearCanReceive) { ubEepromAnswerReceiveMem = ubGearCanReceive; WriteGearEeprom(ubGearCanReceive); WriteTurbineDirEeprom(fTubineDirectionReceive); } /*EEPROM pitch position*/ if(fLastCanPitchPercent != fCanPitchPercent) { fLastCanPitchPercent = fCanPitchPercent; WritePitchEeprom(fLastCanPitchPercent); } chinookpack_pack_float(&pk,sSensorValues.fWheelRPM); Set_Timeout(); send_CAN_msg(&sCanMsgWheelRPM,bTxCanBuffer, 5); while(!is_CAN_msg_send(&sCanMsgWheelRPM) && !sSystemFlags.CanTimeout); Reset_Timeout(); chinookpack_fbuffer_clear(&buf); chinookpack_pack_float(&pk,sSensorValues.fWindSpeed); Set_Timeout(); send_CAN_msg(&sCanMsgWindSpeed,bTxCanBuffer, 5); while(!is_CAN_msg_send(&sCanMsgWindSpeed) && !sSystemFlags.CanTimeout); Reset_Timeout(); chinookpack_fbuffer_clear(&buf); chinookpack_pack_float(&pk,sSensorValues.fWindDir); Set_Timeout(); send_CAN_msg(&sCanMsgWindDirection,bTxCanBuffer, 5); while(!is_CAN_msg_send(&sCanMsgWindDirection) && !sSystemFlags.CanTimeout); Reset_Timeout(); chinookpack_fbuffer_clear(&buf); chinookpack_pack_float(&pk,sSensorValues.fTrust); Set_Timeout(); send_CAN_msg(&sCanMsgTrust,bTxCanBuffer, 5); while(!is_CAN_msg_send(&sCanMsgTrust) && !sSystemFlags.CanTimeout); Reset_Timeout(); chinookpack_fbuffer_clear(&buf); print = 0; } /*Error processing*/ if(sSystemFlags.CanError) { LED_STAT0 = 1; } } return 0; }
int main(void) { fujin_init_board(); // CAN PREPARATION #if ENABLE_CAN == TRUE chinookpack_fbuffer_init(&buf,bTxCanBuffer,8); chinookpack_packer_init(&pk,&buf,chinookpack_fbuffer_write); chinookpack_unpacked_init(&unpacker); config_CAN_Tx_msg(&can_msg_current, CAN_MSG_CURRENT_MONITOR_SID , STANDARD_ID, 3); config_CAN_Tx_msg(&can_msg_voltage, CAN_MSG_VOLTAGE_MONITOR_SID , STANDARD_ID, 3); config_CAN_Tx_msg(&can_msg_clock, CAN_MSG_TIME_SID , STANDARD_ID, 3); //config_CAN_Tx_msg(&can_msg_pitch, CAN_MSG_PITCH_AUTO_SID , STANDARD_ID, 3); config_CAN_Tx_msg(&can_msg_gear, CAN_MSG_GEAR_FUJIN_SID , STANDARD_ID, 3); setup_can_rx(); #endif // Read Clock for timestamps #if ENABLE_RTC == TRUE // init the rtc #endif // TODO: Read Eeprom Parameters // and and set settings // Skadi skadi_init(&fujin.skadi, testCommandTable,sizeof(testCommandTable)/sizeof(SkadiCommand)); UartSetRXLineEvt(UART_1,uartReceiveLineEvt); UartSetRXLineEvt(UART_2,xbeeReceiveLineEvt); while(1){ // 1. Read Current and SHUT DOWN RELAY if overloading #if ENABLE_VMON == TRUE && ENABLE_I2C == TRUE fujin.chinook.power.v = ltc4151_read_voltage(&(fujin.ltc4151_state)); fujin.chinook.power.i = ltc4151_read_current(&(fujin.ltc4151_state)); #endif // 2. Read Clock for timestamps #if ENABLE_RTC == TRUE // At some interval read rtc and send time on can #endif // 2. Read Clock for timestamps #if ENABLE_RTC == TRUE // At some interval read rtc and send time on can #endif // 4. UART Processing #if ENABLE_UART == TRUE // 4.1 XBEE Processing #if ENABLE_XBEE == TRUE if(xbeeline_rcv_new){ skadi_process_command(&fujin.skadi,xbeeline_rcv); xbeeline_rcv_new=0; } #endif // 4.2 USB-Serial Processing #if ENABLE_USBSERIAL == TRUE if(uartline_rcv_new){ skadi_process_command(&fujin.skadi,uartline_rcv); uartline_rcv_new=0; } #endif #endif if(print) { #if ENABLE_XBEE == TRUE && ENABLE_UART == TRUE sprintf(ubDataLoggingBuffer,"%u %.2f %.2f %.2f %.2f %.2f\n\r" ,fujin.loggin.ubGear ,fujin.loggin.fWindSpeed ,fujin.loggin.fTurbineRPM ,fujin.loggin.fWheelRPM ,fujin.loggin.fTrust ,fujin.loggin.fPitch); UartTxFrame(UART_1, ubDataLoggingBuffer, strlen(ubDataLoggingBuffer)); #endif // 3. CAN Processing #if ENABLE_CAN == TRUE chinookpack_pack_float(&pk,fujin.chinook.power.i); Set_Timeout(); send_CAN_msg(&can_msg_current, bTxCanBuffer, 5); while(!is_CAN_msg_send(&can_msg_current) && !CanTimeout); // test si le message est envoyé Reset_Timeout(); chinookpack_fbuffer_clear(&buf); chinookpack_pack_float(&pk,fujin.chinook.power.v); Set_Timeout(); send_CAN_msg(&can_msg_voltage, bTxCanBuffer, 5); while(!is_CAN_msg_send(&can_msg_voltage) && !CanTimeout); // test si le message est envoyé Reset_Timeout(); chinookpack_fbuffer_clear(&buf); #endif print = 0; } } // Should never go there return 0; }
int main(){ int memo_bit_0[4]={0, 0, 0, 0}, memo_bit_1[4]={0, 0, 0, 0}; //Utilisé pour détecter les fronts (up/down) des boutons unsigned int memo_cran_levier=0; //Mémorise dans quel cran se trouve le levier /* Paramète d'affichage de l'ADC du pitch variable */ float BUFF[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // initialise 16 BUFFER float OFFSET[16]={0.005f,0,0.005f,0,0.005f,0,0.005f,0,0.005f,0,0.005f,0,0.005f,0,0.005f,0}; // ajout d'un offset pour un réglage précis int BUFF_val[16]={1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // 1 = affichage du buffer float Pot_Pitch_Volt=0; //valeur en volt du potentiomètre pour les pitchs variables unsigned int *Levier_num_cran=0; //numéro du cran dans lequel se trouve le levier pour le pitch variable Setup(); //Initialisation PIC, UART,... /* initialisation de l'encodeur et can*/ initialisation_CAN(); initialisation_message_Tx_CAN(); chinookpack_fbuffer_init(&buf, dataArray, 8); //initialisation des buffer chinookpack_packer_init(&pk, &buf, chinookpack_fbuffer_write); //initialisation du pack pour le message Init_Timer3(TIMER3_FREQ); //CAN timeout timer while(1) { Analyse_ADC(BUFF, OFFSET, BUFF_val); //Analyse les entrées analogiques Pot_Pitch_Volt = BUFF[0]; //recopie de la valeur du potentiomètre if(variation_pitch(Pot_Pitch_Volt, &Levier_num_cran)){ //Envoie du message commande des pitchs variables seulement s'il y a eu un changement de cran sur le levier chinookpack_pack_uint8(&pk, Levier_num_cran); Set_Timeout(); send_CAN_msg(&can_msg_manual_pitch, dataArray, 2); // envoie du message Can_manual_pitch while(is_CAN_msg_send(&can_msg_manual_pitch) != TRUE && !can_time_out); // test si le message est envoyé Reset_Timeout(); } chinookpack_fbuffer_clear(&buf); /*détecte si un bouton change et envoie des messages can seulement s'il y a changement sur un bouton*/ if((!BP1 && !memo_bit_1[0]) || (!BP2 && !memo_bit_1[1]) || (!BP3 && !memo_bit_1[2]) ||(!BP4 && !memo_bit_1[3])){ if(!BP1) {memo_bit_0[0]=0;memo_bit_1[0]=1;Led1=1;Nop();} // mémorise le bouton 1 comme étant à l'état haut if(!BP2) {memo_bit_0[1]=0;memo_bit_1[1]=1;Led2=1;Nop();} // mémorise le bouton 2 comme étant à l'état haut if(!BP3) {memo_bit_0[2]=0;memo_bit_1[2]=1;Led3=1;Nop();} // mémorise le bouton 3 comme étant à l'état haut if(!BP4) {memo_bit_0[3]=0;memo_bit_1[3]=1;Led4=1;Nop();} // mémorise le bouton 4 comme étant à l'état haut chinookpack_pack_uint8(&pk,(memo_bit_0[3]<<3)|(memo_bit_0[2]<<2)|(memo_bit_0[1]<<1)|(memo_bit_0[0])); //concatène les quatre boutons Set_Timeout(); send_CAN_msg(&can_msg_button_cmd, dataArray, 2); // Envoie du message CAN des boutons while(is_CAN_msg_send(&can_msg_button_cmd) != TRUE && !can_time_out); // test si le message est envoyé Reset_Timeout(); } else if((BP1 && !memo_bit_0[0]) || (BP2 && !memo_bit_0[1]) || (BP3 && !memo_bit_0[2]) ||(BP4 && !memo_bit_0[3])){ if(BP1) {memo_bit_0[0]=1;memo_bit_1[0]=0;Led1=0;Nop();} // mémorise le bouton 1 comme étant à l'état bas if(BP2) {memo_bit_0[1]=1;memo_bit_1[1]=0;Led2=0;Nop();} // mémorise le bouton 2 comme étant à l'état bas if(BP3) {memo_bit_0[2]=1;memo_bit_1[2]=0;Led3=0;Nop();} // mémorise le bouton 3 comme étant à l'état bas if(BP4) {memo_bit_0[3]=1;memo_bit_1[3]=0;Led4=0;Nop();} // mémorise le bouton 4 comme étant à l'état bas chinookpack_pack_uint8(&pk,(memo_bit_0[3]<<3)|(memo_bit_0[2]<<2)|(memo_bit_0[1]<<1)|(memo_bit_0[0])); //concatène les quatre boutons Set_Timeout(); send_CAN_msg(&can_msg_button_cmd, dataArray, 2); // Envoie du message CAN des boutons while(is_CAN_msg_send(&can_msg_button_cmd) != TRUE && !can_time_out); // test si le message est envoyé Reset_Timeout(); } chinookpack_fbuffer_clear(&buf); // vide le buffer pour le pack de l'encodeur message can }//while }