Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma config(Sensor, S1, touchS, sensorTouch)
- #pragma config(Sensor, S3, lightS1, sensorLightInactive)
- #pragma config(Sensor, S4, lightS2, sensorLightInactive)
- #pragma config(Motor, motorA, aMotor, tmotorNormal, PIDControl, )
- #pragma config(Motor, motorB, bMotor, tmotorNormal, PIDControl, )
- //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
- const tSensors touchS = (tSensors) S1;
- const tSensors lightS1 = (tSensors) S3;
- const tSensors lightS2 = (tSensors) S4;
- static int READ_WAIT = 100;
- static int SPREMEMBA_SMERI_MOGOCE = 10; // ce 10*READ_WAIT ni nic
- static const int CAPACITY = 1200;
- static int RecLength = CAPACITY;
- static const int SPEED_A = 60;
- static const int SPEED_LINE = 20;
- static int prenosA = 0;
- static int prenosB = 0;
- static int KOT_45 = 210;
- static int KOT_45_POL = 105;
- static const int BUF_AMOUNT = KOT_45 + KOT_45;
- static const bool REVERSE = false;
- static const bool PORAVNAVA_NA_CRTO = true;
- static const int LIGHT_DIFF = 50;
- static const int DIRECTION_FORWARD = -1;
- static const int LIGHT_CHECKPOINT_DISTANCE = 0; // kdaj preverjamo
- static const int LIGHT_SENSORS_CHECKPOINT = 20; // manj od tolko da zabelezimo checkpoint
- static int wait4me = false;
- static int checkpoints[20];
- static int rotA[CAPACITY];
- static int rotB[CAPACITY];
- static int currentPositionPlay = 0;
- void init()
- {
- //nMaxRegulatedSpeedNxt = 750;
- //nMaxRegulatedSpeed12V = 750;
- nMotorPIDSpeedCtrl[motorA] = mtrSpeedReg;
- if (REVERSE) bMotorFlippedMode[motorA]=true;
- motor[motorA]=100;
- motor[motorA]=0;
- nMotorPIDSpeedCtrl[motorB] = mtrSpeedReg;
- if (REVERSE) bMotorFlippedMode[motorB]=true;
- motor[motorB]=100;
- motor[motorB]=0;
- }
- void print(int i, int linenr, int wait=1000)
- {
- nxtDisplayBigTextLine(linenr, "%d ", i);
- wait1Msec(wait);
- }
- void print(int i, int i2, int linenr, int wait)
- {
- nxtDisplayBigTextLine(linenr, "%d %d ", i, i2);
- wait1Msec(1000);
- }
- int deli(int r, int cifra, int cifrapol)
- {
- int res = 0;
- int pz = 1;
- if (r < 0) {
- pz = -1;
- r = -r;
- }
- while (r > cifra) {
- res++;
- r -= cifra;
- }
- if (r > cifrapol)
- res++;
- return pz*res;
- }
- void trackBoth()
- {
- int i = 0;
- int smer = 0;
- nMotorEncoder[motorA]=0;
- nMotorEncoder[motorB]=0;
- int bufA = 0;
- int bufB = 0;
- int checkpointIdx = 0;
- int nothing_i = SPREMEMBA_SMERI_MOGOCE+1;
- for (; i < CAPACITY; )
- {
- nxtDisplayBigTextLine(4, "I:%d ", i);
- wait1Msec(READ_WAIT);
- bool isNothing = ((nMotorEncoder[motorA] == 0) && (nMotorEncoder[motorB] == 0));
- if (isNothing)
- {
- nothing_i++;
- }
- if ((smer == 1 || smer == 2)) {
- int x;
- if (smer == 1)
- x = bufA;
- else
- x = bufB;
- x = deli(x, KOT_45, KOT_45_POL);
- x = x * 45;
- nxtDisplayBigTextLine(2, "KOT:%d* ", x);
- }
- if (nothing_i >= SPREMEMBA_SMERI_MOGOCE) {
- nxtDisplayBigTextLine(0, "SMER? ");
- PlaySound(soundBlip);
- }
- if (SensorValue(lightS1) < LIGHT_SENSORS_CHECKPOINT && SensorValue(lightS2) < LIGHT_SENSORS_CHECKPOINT)
- {
- nxtDisplayBigTextLine(6, "CHK%d ", checkpointIdx);
- if (checkpointIdx < 1)
- checkpoints[checkpointIdx++] = i;
- else if (checkpoints[checkpointIdx-1] != i)
- checkpoints[checkpointIdx++] = i;
- wait1Msec(1000);
- nxtDisplayBigTextLine(6, " ");
- }
- if (isNothing && (SensorValue(touchS) == 0)) continue;
- if ((nothing_i >= SPREMEMBA_SMERI_MOGOCE) || (abs(bufA) >= BUF_AMOUNT) || (abs(bufB) >= BUF_AMOUNT) || ((SensorValue(touchS) != 0) && (i > 0)))
- {
- if (bufA != 0 || bufB != 0)
- {
- // 470 = pravi kot
- // 235 = 45*
- if (smer == 1) {
- bufA = deli(bufA, KOT_45, KOT_45_POL) * KOT_45;
- rotA[i] = bufA;
- rotB[i] = 0;
- wait1Msec(1000);
- }
- else if (smer == 2) {
- bufB = deli(bufB, KOT_45, KOT_45_POL) * KOT_45;
- rotA[i] = 0;
- rotB[i] = bufB;
- }
- else if (smer == 0) {
- rotA[i] = bufA;
- rotB[i] = bufA;
- }
- i++;
- bufA = 0;
- bufB = 0;
- }
- }
- if (SensorValue(touchS) != 0) break;
- if (!isNothing && nothing_i >= SPREMEMBA_SMERI_MOGOCE)
- {
- nothing_i = 0;
- wait1Msec(READ_WAIT*10);
- int rA = abs(nMotorEncoder[motorA]);
- int rB = abs(nMotorEncoder[motorB]);
- if ((rA / 3) > rB) // obracanje
- {
- smer = 1; // desno
- } else if ((rB / 3) > rA) // obracanje
- {
- smer = 2; // levo
- } else {
- smer = 0;
- }
- if (smer == 0)
- nxtDisplayBigTextLine(0, "FORW ");
- else if (smer == 1)
- nxtDisplayBigTextLine(0, "LEFT ");
- else if (smer == 2)
- nxtDisplayBigTextLine(0, "RIGHT ");
- }
- bufA += nMotorEncoder[motorA];
- bufB += nMotorEncoder[motorB];
- nMotorEncoder[motorA]=0;
- nMotorEncoder[motorB]=0;
- }
- if (i > 1) // remember before
- RecLength = i;
- }
- void waitMotor(int motorN, int rot)
- {
- if (rot < 0)
- { // max 5s
- for (int i=0; i<500 && nMotorEncoder[motorN] > rot; i++) {
- wait1Msec(10);
- if (wait4me) {
- while (wait4me) { abortTimeslice(); }
- }
- }
- } else if (rot > 0)
- {
- for (int i=0; i<500 && nMotorEncoder[motorN] < rot; i++) {
- wait1Msec(10);
- if (wait4me) {
- while (wait4me) { abortTimeslice(); }
- }
- }
- }
- }
- void getMotorDiff(int rotA, int rotB)
- {
- prenosA = rotA - nMotorEncoder[motorA];
- prenosB = rotB - nMotorEncoder[motorB];
- nMotorEncoder[motorA] = 0;
- nMotorEncoder[motorB] = 0;
- }
- void driveMotor(int motorN, int rot)
- {
- if (rot == 0) return;
- if (rot < 0) {
- if (motorN == (int) motorA) {
- nMotorEncoder[motorN] = rot + prenosA;
- } else {
- nMotorEncoder[motorN] = rot + prenosB;
- }
- nMotorEncoderTarget[motorN] = 0;
- motor[motorN] = -SPEED_A;
- } else {
- nMotorEncoder[motorN] = 0;
- if (motorN == (int) motorA) {
- nMotorEncoderTarget[motorN] = -rot - prenosA;
- } else {
- nMotorEncoderTarget[motorN] = -rot - prenosB;
- }
- motor[motorN] = SPEED_A;
- }
- prenosA = 0;
- prenosB = 0;
- }
- void driveBoth() {
- for(currentPositionPlay = 0; currentPositionPlay < RecLength; currentPositionPlay++){
- nxtDisplayBigTextLine(2, "I:%d ", currentPositionPlay);
- int rA = rotA[currentPositionPlay];
- int rB = rotB[currentPositionPlay];
- nSyncedMotors = synchNone;
- if (rA == rB && rA == 0) continue;
- if (abs(rA) > 0 && abs(rB) > 0) {
- driveMotor(motorA, rA);
- driveMotor(motorB, rA);
- waitMotor(motorA, rA);
- getMotorDiff(rA, rA);
- } else if (abs(rA) > 0) {
- driveMotor(motorA, rA);
- waitMotor(motorA, rA);
- getMotorDiff(rA, rB);
- } else {
- driveMotor(motorB, rB);
- waitMotor(motorB, rB);
- getMotorDiff(rA, rB);
- }
- motor[motorA] = 0;
- motor[motorB] = 0;
- }
- nSyncedMotors = synchNone;
- motor[motorA] = 0;
- motor[motorB] = 0;
- }
- void controlFloatMotors(bool val)
- {
- bFloatDuringInactiveMotorPWM=val;
- motor[motorA] = 100;
- motor[motorB] = 100;
- motor[motorA] = 0;
- motor[motorB] = 0;
- }
- void blinkLights(int time)
- {
- SensorType[lightS1] = sensorLightActive;
- SensorType[lightS2] = sensorLightActive;
- wait1Msec(time);
- SensorType[lightS1] = sensorLightInactive;
- SensorType[lightS2] = sensorLightInactive;
- }
- task poravnava()
- {
- wait1Msec(500);
- while (true)
- {
- int v1 = SensorValue(lightS1);
- int v2 = SensorValue(lightS2);
- bool checking = false;
- for (int i=0; i<20; i++)
- {
- if (checkpoints[i] == -100) break;
- if (checkpoints[i] >= (currentPositionPlay-LIGHT_CHECKPOINT_DISTANCE) &&
- checkpoints[i] <= (currentPositionPlay+LIGHT_CHECKPOINT_DISTANCE))
- {
- checking = true;
- }
- }
- if (checking
- && (abs(SensorValue(lightS1)-SensorValue(lightS2)) > LIGHT_DIFF))
- {
- hogCPU();
- wait4me = true;
- int cacM1 = motor[motorA];
- int cacM2 = motor[motorB];
- motor[motorA] = 0;
- motor[motorB] = 0;
- int motorN = motorA;
- int motorO = motorB;
- if (v1 < v2) {
- motorN = motorB;
- motorO = motorA;
- }
- int cacME1 = nMotorEncoder[motorA];
- int cacMET1 = nMotorEncoderTarget[motorA];
- int cacME2 = nMotorEncoder[motorB];
- int cacMET2 = nMotorEncoderTarget[motorB];
- blinkLights(100);
- nMotorEncoderTarget[motorN] = 0;
- motor[motorN] = SPEED_LINE * DIRECTION_FORWARD;
- nMotorEncoderTarget[motorO] = 0;
- motor[motorO] = -SPEED_LINE * DIRECTION_FORWARD;
- nMotorEncoder[motorA] = 0;
- nMotorEncoder[motorB] = 0;
- while (abs(SensorValue(lightS1)-SensorValue(lightS2)) > LIGHT_DIFF) {
- nxtDisplayBigTextLine(6, "DIFF:%d ", abs(SensorValue(lightS1)-SensorValue(lightS2)));
- wait1Msec(50);
- }
- nxtDisplayBigTextLine(6, " ");
- nMotorEncoder[motorA] = cacME1;
- nMotorEncoderTarget[motorA] = cacMET1;
- motor[motorA] = cacM1;
- nMotorEncoder[motorB] = cacME2;
- nMotorEncoderTarget[motorB] = cacMET2;
- motor[motorB] = cacM2;
- wait4me = false;
- releaseCPU();
- blinkLights(100);
- wait1Msec(1000);
- }
- wait1Msec(100);
- }
- }
- task main()
- {
- nxtDisplayBigTextLine(0, "INIT ");
- for(int i=0; i<CAPACITY; i++){
- rotA[i]=0;
- rotB[i]=0;
- if (i < 20) checkpoints[i] = -100;
- }
- init();
- RecLength = 1;
- rotA[0] = -1200;
- rotB[0] = -1200;
- wait1Msec(500);
- while (true) {
- controlFloatMotors(true);
- wait1Msec(500);
- nxtDisplayBigTextLine(0, "RDY ");
- while(SensorValue(touchS) == 0);
- nxtDisplayBigTextLine(0, "WAIT ");
- wait1Msec(500);
- nxtDisplayBigTextLine(0, "REC ");
- trackBoth();
- nxtDisplayBigTextLine(4, " ");
- nxtDisplayBigTextLine(6, " ");
- nxtDisplayBigTextLine(0, "WAIT ");
- wait1Msec(500);
- nxtDisplayBigTextLine(0, "RDY ");
- while(SensorValue(touchS)==0);
- nxtDisplayBigTextLine(0, "PLAY ");
- controlFloatMotors(false);
- wait1Msec(1000);
- if (PORAVNAVA_NA_CRTO) StartTask(poravnava);
- driveBoth();
- if (PORAVNAVA_NA_CRTO) StopTask(poravnava);
- nxtDisplayBigTextLine(0, "DONE ");
- while(SensorValue(touchS)==0);
- nxtDisplayBigTextLine(0, "WAIT ");
- }
- }
Add Comment
Please, Sign In to add comment