/* * Send ticks to local client services */ void CMirrorService::masterTick() { // 1. Send TICK to local client services CTickProxy::sendTicks(); doNextTask(); }
/* * Advance state in the mirror automaton */ void CMirrorService::doNextTask() { if ( ! _DeltaSent ) { if ( CTickProxy::checkIfAllClientTocksReceived() && checkIfAllATAcknowledgesReceived() ) { // Advance to step 3. buildAndSendAllOutgoingDeltas(); nlassert( _DeltaSent ); // Advance to step 4 if the required conditions are met. BeginWaitForAllDeltaReceivedTime = CTime::getLocalTime(); doNextTask(); } } else { if ( checkIfAllIncomingDeltasReceived() ) // { // Advance to step 4. CTickProxy::TimeMeasures.MirrorMeasure[WaitAllReceivedDeltaDuration] = (uint16)(CTime::getLocalTime() - BeginWaitForAllDeltaReceivedTime); nlassert( CTickProxy::checkIfAllClientTocksReceived() ); tockMasterAndProcessReceivedIncomingDeltas(); nlassert( ! CTickProxy::checkIfAllClientTocksReceived() ); } else /*if ( ! checkIfAllIncomingDeltasReceived() )*/ { // Shouldn't be negative if ( _NbExpectedDeltaUpdates-_NbDeltaUpdatesReceived < 0 ) nlwarning( "More delta updates received than expected: E%u R%u", _NbExpectedDeltaUpdates, _NbDeltaUpdatesReceived ); //nldebug( "Delta updates remaining: %hd", _NbExpectedDeltaUpdates-_NbDeltaUpdatesReceived ); } } }
/* * Increment _NbDeltaUpdatesReceived for the number of times we have received a VALID delta update before sync * (valid = its tick = the current tick + 1) */ void CMirrorService::examineDeltaUpdatesReceivedBeforeSync() { if ( ! _GameCyclesOfDeltaReceivedBeforeSync.empty() ) { for ( vector<TGameCycle>::const_iterator ig=_GameCyclesOfDeltaReceivedBeforeSync.begin(); ig!=_GameCyclesOfDeltaReceivedBeforeSync.end(); ++ig ) { if ( (*ig) > CTickProxy::getGameCycle() ) ++_NbDeltaUpdatesReceived; } _GameCyclesOfDeltaReceivedBeforeSync.clear(); doNextTask(); } }
////////////////////////////////////// // Main ///////////////////////////////////// int main(void) { //int a = 0; ///////////////////////////////////// ///TODO////////////////////////////// ///////////////////////////////////// // //participer a la coupe ///////////////////////////////////// ///////////////////////////////////// //Initialisations//////////////////// ///////////////////////////////////// uart_init(); time_init(128); printf("fdevopen \n"); fdevopen(uart0_send,uart0_recv, 1); // Affichage uart: tip -s 9600 -l /dev/ttyS0 // prog usb : // avarice -j /dev/ttyUSB0 --erase --program -f ./main.hex adc_init(); DDRE |= 0x00; PORTE |= 0x10;//active le pull up //initialisation du fpga sbi(XMCRA,SRW11); sbi(XMCRA,SRW00); sbi(MCUCR,SRE); wait_ms(1000); //on reset le fpga U_RESET = 0x42; sbi(PORTB,0); wait_ms(100); //on relache le reset cbi(PORTB,0); sbi(PORTE,3); wait_ms(100); //printf("stack begin :%d \n", &a); //init des interruptions printf("init i2c \n"); scheduler_init(); position_init(&pos); asserv_init(&asserv,&pos); trajectory_init(&traj,&pos,&asserv); i2cm_init(); //autorise les interruptions sei(); //infinite loop!!! //get_enc_value(); //init communication #ifdef ENABLE_MECA printf("init com \n"); initCom(); printf("end init com \n"); #endif //init A* initObstacle(); //init méca #ifdef ENABLE_MECA mecaCom(TIROIR_FERMER); mecaCom(PEIGNE_FERMER); #endif team = getTeam(); while(MAXIME);//boucle anti_maxime // while(1) // { // printf("gp2r = %d ,gp2l = %d ,gp2m = %d \n",adc_get_value(ADC_REF_AVCC | MUX_ADC0),adc_get_value(ADC_REF_AVCC | MUX_ADC1),adc_get_value(ADC_REF_AVCC | MUX_ADC2)); // } //avoidance_init(); //antipatinage_init(); // homologation(); // while(1); // test_evitement(); // while(1); if(team == RED) { disableSpinning(); findPosition(team); enableSpinning(); trajectory_goto_d(&traj, END, 20); trajectory_goto_a(&traj, END, 55); while(!trajectory_is_ended(&traj)); // // trajectory_goto_a(&traj, END, 90); // // trajectory_goto_a(&traj, END, 0); // while(!trajectory_is_ended(&traj)); asserv_set_vitesse_normal(&asserv); initTaskManager(&tkm); //addTask(&tkm, &returnFire, LOW_PRIORITY, R1); addTask(&tkm, &throwSpears, LOW_PRIORITY, RED); addTask(&tkm, &putPaint, HIGH_PRIORITY, team); // addTask(&tkm, &returnFire, LOW_PRIORITY, R3); // addTask(&tkm, &returnFire, HIGH_PRIORITY, R2); addTask(&tkm, &takeFruitRed, HIGH_PRIORITY, team); // addTask(&tkm, &takeFruitYellow, HIGH_PRIORITY, team); // addTask(&tkm, &findPosition, HIGH_PRIORITY, REDPAINT); // addTask(&tkm, &returnFire, HIGH_PRIORITY, Y1); //addTask(&tkm, &throwSpears, HIGH_PRIORITY, YELLOW); addTask(&tkm, &putFruit, HIGH_PRIORITY, team); // addTask(&tkm, &returnFire, HIGH_PRIORITY, Y3); // addTask(&tkm, &takeFruitYellow, HIGH_PRIORITY, team); // addTask(&tkm, &putFruit, HIGH_PRIORITY, team); // addTask(&tkm, &returnFire, HIGH_PRIORITY, Y2); while (TIRETTE); printf("begin match \n"); avoidance_init(); while(doNextTask(&tkm)); printf("finish \n"); } else // Team jaune { disableSpinning(); findPosition(team); enableSpinning(); trajectory_goto_d(&traj, END, 20); trajectory_goto_a(&traj, END, -55); while(!trajectory_is_ended(&traj)); // addTask(&tkm, &returnFire, LOW_PRIORITY, R1); addTask(&tkm, &returnFire, LOW_PRIORITY, Y2); addTask(&tkm, &throwSpears, LOW_PRIORITY, YELLOW); addTask(&tkm, &putPaint, HIGH_PRIORITY, team); // addTask(&tkm, &returnFire, HIGH_PRIORITY, Y3); // addTask(&tkm, &takeFruitRed, HIGH_PRIORITY, team); addTask(&tkm, &takeFruitYellow, HIGH_PRIORITY, team); // addTask(&tkm, &returnFire, LOW_PRIORITY, R1); addTask(&tkm, &putFruit, HIGH_PRIORITY, team); // addTask(&tkm, &findPosition, HIGH_PRIORITY, YELLOWPAINT); //addTask(&tkm, &returnFire, HIGH_PRIORITY, R1); //addTask(&tkm, &throwSpears, HIGH_PRIORITY, RED); // addTask(&tkm, &returnFire, HIGH_PRIORITY, R3); // addTask(&tkm, &takeFruitRed, HIGH_PRIORITY, team); // addTask(&tkm, &putFruit, HIGH_PRIORITY, team); // addTask(&tkm, &returnFire, HIGH_PRIORITY, R2); while (TIRETTE); printf("begin match \n"); avoidance_init(); while(doNextTask(&tkm)); printf("finish \n"); // // attend que la tirette soit retirée // printf("begin match \n"); // printf("start pos: x %lf pos y %lf \n",position_get_x_cm(&pos),position_get_y_cm(&pos)); } printf("time: %ld \n",time_get_s()); while(1);//attention boucle infinie }
void CMirrorService::receiveDeltaFromRemoteMS( CMessage& msgin, TServiceId senderMSId ) { H_AUTO(receiveDeltaFromRemoteMS); // 2b. Receive delta updates from remote MSs => store them in pending deltas _PendingDeltas.push_back( make_pair( senderMSId, msgin ) ); // warning: msgin must be copied because below we advance the reading position of msgin //nldebug( "Received delta from %s", servStr(senderMSId).c_str() ); // Get the "wait for" mode of this remote MS delta bool waitForIt; msgin.serial( waitForIt ); if ( waitForIt ) // we assume it does not change during the lifetime of a particular remote MS { // Check if the sender is a new remote MS (search in the "delta update expectation list") std::list< TServiceId >::const_iterator it = find( _RemoteMSToWaitForDelta.begin(), _RemoteMSToWaitForDelta.end(), senderMSId ); if ( it == _RemoteMSToWaitForDelta.end() ) { _RemoteMSToWaitForDelta.push_back( senderMSId ); // _RemoteMSToWaitForDelta will not contain remote MSes for which we don't have to wait ++_NbExpectedDeltaUpdates; } } TGameCycle gamecycle; msgin.serial( gamecycle ); //nldebug( "%u: Rcvd Delta %u", CTickProxy::getGameCycle(), gamecycle ); _QuickLog.displayNL( "TCK-%u: Rcvd Delta %u from MS-%hu (NbExpectedDelta=%hd)", CTickProxy::getGameCycle(), gamecycle, senderMSId.get(), _NbExpectedDeltaUpdates ); // Count this delta update for the wait test only if its tick is the current tick or +1 (see explanation below) // Explanation: possibles cases: // // MS1 MS2 // | | // Tick1>| Tick1>| // Delta>|>Delta <====> Delta>|>Delta // |>Tock1 |>Tock1 // | | // | Tick2>| // Delta>| <----- |>Delta // Tick2>|>Delta -----> Delta>|>Tock2 // |>Tock2 | // if ( CTickProxy::State == ExpectingMasterTick ) { // If sync not received yet, defer possible incrementation of _NbDeltaUpdatesReceived // because we can't compare now the gamecycle of the delta update with the current gamecycle // (see examineDeltaUpdatesReceivedBeforeSync() called when receiving the sync) if ( CTickProxy::getGameCycle() == 0 ) { if ( waitForIt ) _GameCyclesOfDeltaReceivedBeforeSync.push_back( gamecycle ); return; } // Accept +1 only if received by advance (before the tick update) // Forbid >+1 // Discard <= else if ( gamecycle > CTickProxy::getGameCycle() ) { nlassert( gamecycle == CTickProxy::getGameCycle() + 1 ); if ( waitForIt ) ++_NbDeltaUpdatesReceived; } } else { // Accept == if received in the same tick // Forbid > // Discard < if ( gamecycle >= CTickProxy::getGameCycle() ) { nlassert( gamecycle == CTickProxy::getGameCycle() ); if ( waitForIt ) ++_NbDeltaUpdatesReceived; } } doNextTask(); }