/* * app_ck_proms() * Append all those promotion moves to the list (out of all 4), * which are able to check on a direction "ckdir". */ static void app_ck_proms( register Movelist* lp, Position pos, Position tpos, int8 idx, register int ckdir) { Move m; m.m_value = 0; m.m_attr = 0; m.m_from = pos; m.m_to = tpos; m.m_idx = idx; if( dam_dir(ckdir) ) { m.m_fig = dame; app_move(&m, lp); if( lfr_dir(ckdir) ) { m.m_fig = laeufer; app_move(&m, lp); } else { m.m_fig = turm; app_move(&m, lp); } } else if( spr_dir(ckdir) ) { m.m_fig = springer; app_move(&m, lp); } }
static void sol_1move_list( Move* mp, Bool valid, Movelist* sols ) { clear_list(sols); if( mp && valid && !no_pos(mp->m_from) ) { app_move(mp, sols); } mg_link(sols); }
/* * mg_app_castle() * Append the legal castling moves to the list. * No entry conditions are really necessary. */ Eximpl void mg_app_castle( register Xconst Board* bp, register Movelist* lp) { register Xconst Field* p; register PieceSet emask; register rPosition kpos; Move m; emask = COLOUR_MASK(opp_colour(bp->b_tomove)); kpos = K_POS(bp, bp->b_tomove); p = &(bp->b_f[kpos]); if( ! (F_DATT(p) & emask) ) { m.m_from = kpos; m.m_fig = p->f_f; m.m_idx = p->f_idx; m.m_value = 0; m.m_attr = 0; if( bp->b_castle[bp->b_tomove] & castle00 ) { if( (p[MK_DELTA(1,0)].f_c == empty) && (p[MK_DELTA(2,0)].f_c == empty) && ! (F_DATT(&(p[MK_DELTA(1,0)])) & emask) && ! (F_DATT(&(p[MK_DELTA(2,0)])) & emask) ) { m.m_to = kpos + MK_DELTA(2,0); app_move(&m, lp); } } if( bp->b_castle[bp->b_tomove] & castle000 ) { if( (p[-MK_DELTA(1,0)].f_c == empty) && (p[-MK_DELTA(2,0)].f_c == empty) && (p[-MK_DELTA(3,0)].f_c == empty) && ! (F_DATT(&(p[-MK_DELTA(1,0)])) & emask) && ! (F_DATT(&(p[-MK_DELTA(2,0)])) & emask) ) { m.m_to = kpos - MK_DELTA(2,0); app_move(&m, lp); } } } }
/* * app_4_proms() * Append all 4 promotion moves to the list. */ static void app_4_proms( register Movelist* lp, Position pos, Position tpos, int8 idx) { Move m; m.m_value = 0; m.m_attr = 0; m.m_from = pos; m.m_to = tpos; m.m_idx = idx; m.m_fig = dame ; app_move(&m, lp); m.m_fig = springer; app_move(&m, lp); m.m_fig = laeufer ; app_move(&m, lp); m.m_fig = turm ; app_move(&m, lp); }
/* * Make a copy of a fully constructed movelist. * If there were not the linkage pointers, we would do a low level * struct copy. Currently: need not be very efficient. */ Eximpl void ml_copy( register const Movelist* src, register Movelist* dst ) { if( dst ) { clear_list(dst); if( src ) { register const Move* mp; formoves(src, mp) { app_move(mp, dst); } dst->l_attr = src->l_attr; dst->l_hasckmv = src->l_hasckmv; mg_link(dst); /* terminate construction */ } }
task main() { int timer_release = 1; int app_timeout = 3000; int stop_functie = 1; bool intersection = false; nVolume = 3; string app_message = ""; wait1Msec(50); // The program waits 50 milliseconds to initialize the light sensor. while(true) // Infinite loop { /** check if there is a message available from app */ nSizeOfMessage = cCmdMessageGetSize(INBOX); if (nSizeOfMessage > kMaxSizeOfMessage){ nSizeOfMessage = kMaxSizeOfMessage; } /** store the message of the app into a variable*/ if (nSizeOfMessage > 0) { nBTCmdRdErrorStatus = cCmdMessageRead(nRcvBuffer, nSizeOfMessage, INBOX); nRcvBuffer[nSizeOfMessage] = '\0'; stringFromChars(app_message, (char *) nRcvBuffer); displayCenteredBigTextLine(4, app_message); if (app_message == "FIRE" && timer_release == 1) { if (stop_functie == 0) { stop_functie = 1; app_message = "STOP"; app_move(app_message, false); continue; } else if (stop_functie == 1){ //motor[motorA] = 0; //motor[motorB] = 0; stop_functie = 0; timer_release = 1; } } } /** check the sonar sensor and stop the robot if it sees sth */ if (stop_functie == 0 && intersection == false && SensorValue[sonar1] < 30) { int i = -15; clearSounds(); while (i < 0) { playSound(soundBeepBeep); motor[motorA] = i; motor[motorB] = i; i += 4; wait1Msec(300); } motor[motorA] = 0; motor[motorB] = 0; stop_functie = true; } /** check the color sensor if it's black stop the robot */ else if (stop_functie == 0 && SensorValue[colorSensor] == BLACKCOLOR && SensorValue[lightSensor] < 50) { int i = -15; clearSounds(); while (i < 0) { playSound(soundException); motor[motorA] = i; motor[motorB] = i; i += 4; wait1Msec(300); } motor[motorA] = 0; motor[motorB] = 0; intersection = true; } /** if the robot is at intersection, wait for a reactie from app The time-out is max "app_timeout" */ else if (stop_functie == 0 && intersection == true) { /* Reset Timer1 value (only the first time (when the timer is free) and check if its reached app_timeout) */ if (timer_release == 1){ ClearTimer(T1); timer_release = 0; } if (timer_release == 0 && time1[T1] < app_timeout) { if (nSizeOfMessage > 0){ /* Release the timer */ timer_release = 1; intersection = false; stop_functie = 1; if (app_message == "FIRE") { stop_functie = 0; } else if (app_message != "UP") { app_move(app_message, true); } } } else if (timer_release == 0 && time1[T1] > app_timeout){ intersection = false; /* Release the timer */ timer_release = 1; } } else if (stop_functie == 0 && intersection == false) { displayCenteredBigTextLine(4, "%d", SensorValue[lightSensor]); motor[motorA] = -22 + (power((float)(60 - SensorValue[lightSensor]) / 12, 3) * 12); motor[motorB] = -22 - (power((float)(60 - SensorValue[lightSensor]) / 12, 3) * 12); playSound(soundBlip); } else if (stop_functie == 1) { app_move(app_message, false); app_message = ""; clearSounds(); } } }
/* * mg_app_ep() * Append all legal e.p. moves to the specified list. * No conditions need be assured. */ Eximpl void mg_app_ep( register Xconst Board* bp, register Movelist* lp) { register rColour self; register rPosition tpos; register rPosition pos; register rPosition kpos; register int i; register Xconst Field* p; register rPieceSet emask; register int dir; Move m; if( no_pos(bp->b_ep) ) { return; /* no (double step) target */ } self = bp->b_tomove; tpos = bp->b_ep + bau_mov[self]; for( i=0 ; i<2 ; ++i ) { pos = tpos - (i ? bau_left : bau_right)[self]; p = &( bp->b_f[pos] ); if( (p->f_c != self) || (p->f_f != bauer) ) { continue; } /* * Well, we have a target for e.p. (made double step), * and a B in the correct position to beat it. * Whether this goes into the move list depends now * on the legality of the move, i.e. * - an existing check must be removed * - no new check must be opened. * When there is an existing check, which is not directly * by the target we cannot remove it by beating e.p. * (topologically impossible). * It is also impossible for a B to say double check. */ kpos = K_POS(bp, self); emask = COLOUR_MASK(opp_colour(self)); if( F_DATT(&(bp->b_f[kpos])) & emask ) { /* self in check */ if( F_DATT(&(bp->b_f[kpos])) & emask & ~SET1(bp->b_f[bp->b_ep].f_idx) ) { continue; /* cannot be removed */ } /* * In direct check by the B to be beaten e.p. * This reduces the possible uncoverings: */ if( dam_dir(att_dir(pos, kpos)) && (F_IATT(&(bp->b_f[kpos])) & emask & F_DATT(p)) ) { /* . * . * B b . * K . . * Pinned vertically => illegal */ continue; } } else { /* self not in check */ /* * When removing just the enemy at "b_ep" would discover * a check on a L line, our king could have been beaten * instead of the double step (=> illegal condition). * Discovering vertically through "b_ep" is impossible * as blocked by our own B. * Discovering horizontally through "b_ep" is possible * through both B, only. * Remains to test uncovering through our own B. */ if( F_IATT(&(bp->b_f[kpos])) & emask & F_DATT(p) ) { dir = att_dir(kpos, pos); if( dam_dir(dir) && (lin_dir(dir) != lin_dir(att_dir(pos,tpos))) && is_pinned(bp, pos) ) { continue; /* pinned, and leaves pinning */ } } if( ! ep_legal(bp, pos, tpos, kpos) ) { continue; } } /* no legality objections to the move */ m.m_from = pos; m.m_to = tpos; m.m_fig = bauer; m.m_idx = p->f_idx; m.m_value = 0; m.m_attr = 0; app_move(&m, lp); } /* loop over left/right */ }
task main() { int timer_release = 1; int app_timeout = 3000; int stop_functie = 0; string app_message = ""; wait1Msec(50); // The program waits 50 milliseconds to initialize the light sensor. playSound(soundException); while(true) // Infinite loop { /** check if there is a message available from app */ nSizeOfMessage = cCmdMessageGetSize(INBOX); if (nSizeOfMessage > kMaxSizeOfMessage){ nSizeOfMessage = kMaxSizeOfMessage; } /** store the message of the app into a variable*/ if (nSizeOfMessage > 0) { nBTCmdRdErrorStatus = cCmdMessageRead(nRcvBuffer, nSizeOfMessage, INBOX); nRcvBuffer[nSizeOfMessage] = '\0'; stringFromChars(app_message, (char *) nRcvBuffer); displayCenteredBigTextLine(4, app_message); if (app_message == "FIRE") { if (stop_functie == 0) { stop_functie = 1; app_message = "STOP"; app_move(app_message, false); continue; } else if (stop_functie == 1){ //motor[motorA] = 0; //motor[motorB] = 0; stop_functie = 0; timer_release = 1; } } } /** check the sonar sensor and stop the robot if it sees sth */ if (stop_functie == 0 && SensorValue[sonar1] < 20) { for(int i = 0; i < 20; i++){ motor[motorA] = -20 + (1 * i); motor[motorB] = -20 + (1 * i); wait1Msec(1000); } } /** check the color sensor if it's black stop the robot */ else if (stop_functie == 0 && SensorValue[colorSensor] == BLACKCOLOR) { motor[motorA] = 0; motor[motorB] = 0; /* Reset Timer1 value (only the first time (when the timer is free) and check if its reached app_timeout */ if (timer_release == 1){ ClearTimer(T1); timer_release = 0; } if (timer_release == 0 && time1[T1] < app_timeout) { if (nSizeOfMessage > 0){ app_message = app_message; app_move(app_message, true) stop_functie = 1; /* Release the timer */ timer_release = 1; } } else if (timer_release == 0 && time1[T1] > app_timeout){ nMotorEncoder[motorA] = 0; while (nMotorEncoder[motorA] > -180) { motor[motorA] = -50; motor[motorB] = -50; } /* Release the timer */ timer_release = 1; } } else if (stop_functie == 0) { displayCenteredBigTextLine(4, "%d", SensorValue[lightSensor]); motor[motorA] = -20 + ((60 - SensorValue[lightSensor])); motor[motorB] = -20 - ((60 - SensorValue[lightSensor])); } else if (stop_functie == 1) { app_move(app_message, false); app_message = ""; } } }
task main() { nVolume = 3; //motor[motorC] = -20; /** PDI Algorithm variables */ int Kp = 138; int Ki = 0; int Kd = 2800; int offset = 56; int Tp = 45; int integral = 0; int lastError = 0; int derivative = 0; int LightValue = 0; int error = 0; int turn = 0; int powerA = 0; int powerB = 0; int app_timeout = 3000; bool stop_functie = true; // if it's false the robot drives by itself and if it's true it drives by the app bool intersection = false; // if true we are at intersection string app_message = ""; // the message from app wait1Msec(50); // The program waits 50 milliseconds to initialize the light sensor. while(true) // Infinite loop { /*** check if there is a message available from app ***/ nSizeOfMessage = cCmdMessageGetSize(INBOX); if (nSizeOfMessage > kMaxSizeOfMessage){ nSizeOfMessage = kMaxSizeOfMessage; } /** store the message of the app into a variable*/ if (nSizeOfMessage > 0) { nBTCmdRdErrorStatus = cCmdMessageRead(nRcvBuffer, nSizeOfMessage, INBOX); nRcvBuffer[nSizeOfMessage] = '\0'; stringFromChars(app_message, (char *) nRcvBuffer); displayCenteredBigTextLine(4, app_message); /* Stop or Start the robot by tapping on "Fire" */ if (app_message == "FIRE" && intersection == false) { if (stop_functie == false) { stop_functie = true; app_message = "STOP"; app_move(app_message, false); continue; } else if (stop_functie == true){ stop_functie = false; } } } /*** if the robot drives by itself ***/ if (stop_functie == false) { /** check the sonar sensor and stop the robot if it see's sth **/ if (intersection == false && SensorValue[sonar1] < 30) { int i = -15; clearSounds(); while (i < 0) { playSound(soundBeepBeep); motor[motorA] = i; motor[motorB] = i; i += 4; wait1Msec(300); } motor[motorA] = 0; motor[motorB] = 0; stop_functie = true; } /** Detect intersections for the first time **/ else if (SensorValue[colorSensor] == BLACKCOLOR && SensorValue[lightSensor] < 50) { playSound(soundException); int i = -20; clearSounds(); while (i < 0) { playSound(soundException); motor[motorA] = i; motor[motorB] = i; i += 4; wait1Msec(300); } motor[motorA] = 0; motor[motorB] = 0; intersection = true; /* Reset Timer1 value (only the first time the robot sees a intersection **/ ClearTimer(T1); } /** if the robot is at intersection, wait for a message from app The time-out is max "app_timeout" **/ else if (intersection == true) { if (time1[T1] < app_timeout) { if (nSizeOfMessage > 0){ intersection = false; stop_functie = true; if (app_message == "FIRE") { stop_functie = false; } else if (app_message != "UP") { app_move(app_message, true); } } } else if (time1[T1] > app_timeout){ intersection = false; } } /** if there is no other condition: (NO intersection, No sonar) **/ else if (intersection == false) { //motor[motorA] = -23 + power((float)(58 - SensorValue[lightSensor]) / 5.15, 3); //motor[motorB] = -23 - power((float)(58 - SensorValue[lightSensor]) / 5.15, 3); //playSound(soundBlip); LightValue = SensorValue(lightSensor); error = LightValue - offset; //integral = integral + error; derivative = error - lastError; Turn = (Kp*error) + (Ki*integral) + (Kd*derivative); Turn = Turn/100; powerA = Tp + Turn; powerB = Tp - Turn; motor[motorA] = -1*powerA; motor[motorB] = -1*powerB; lastError = error; displayCenteredBigTextLine(4, "%d", error); } } /*** if the robot drives by the app ***/ else if (stop_functie == true) { clearSounds(); app_move(app_message, false); app_message = ""; } } }