#pragma config(Sensor, S1, HTCOMPASS, sensorI2CCustom)
#pragma config(Motor, motorB, M_RIGHT, tmotorNormal, PIDControl, reversed)
#pragma config(Motor, motorC, M_LEFT, tmotorNormal, PIDControl, reversed)
#include "drivers/hitechnic-compass.h"
#define WHEELDIST 115 // distance between the wheels
#define WHEELSIZE 56 // diameter of the wheels
#define MOTORSPEED 4 // speed at which motors should turn
task timeMe() {
wait1Msec(20000);
PlaySound(soundBeepBeep);
while(bSoundActive) EndTimeSlice();
}
task showPulse() {
while (true) {
nxtDisplayCenteredBigTextLine(6, " ");
wait1Msec(400);
nxtDisplayCenteredBigTextLine(6, "*");
wait1Msec(400);
}
}
int numRotations() {
return ((WHEELDIST * 3142) / 1000) / ((WHEELSIZE * 3142) / 1000);
}
void startCalibration() {
if (!HTMCstartCal(HTCOMPASS)) {
eraseDisplay();
nxtDisplayTextLine(1, "ERROR: Couldn't");
nxtDisplayTextLine(2, "calibrate sensor.");
nxtDisplayTextLine(4, "Check connection");
nxtDisplayTextLine(5, "and try again.");
PlaySound(soundException);
while(bSoundActive) EndTimeSlice();
wait1Msec(5000);
StopAllTasks();
}
}
void stopCalibration() {
if (!HTMCstopCal(HTCOMPASS)) {
eraseDisplay();
nxtDisplayTextLine(1, "ERROR: Calibration");
nxtDisplayTextLine(2, "has failed.");
nxtDisplayTextLine(4, "Check connection");
nxtDisplayTextLine(5, "and try again.");
PlaySound(soundException);
while(bSoundActive) EndTimeSlice();
wait1Msec(5000);
StopAllTasks();
} else {
nxtDisplayTextLine(1, "SUCCESS: ");
nxtDisplayTextLine(2, "Calibr. done.");
PlaySound(soundUpwardTones);
while(bSoundActive) EndTimeSlice();
wait1Msec(5000);
}
}
task main () {
bFloatDuringInactiveMotorPWM = true;
int numDegrees = 0;
nxtDisplayCenteredTextLine(0, "HiTechnic");
nxtDisplayCenteredBigTextLine(1, "Compass");
nxtDisplayCenteredTextLine(3, "Test 2");
nMotorEncoder[M_RIGHT] = 0;
nMotorEncoder[M_LEFT] = 0;
numDegrees = ((numRotations() * 3) / 2) * 450;
StartTask(timeMe);
startCalibration();
nxtDisplayCenteredTextLine(5, "Calibrating...");
StartTask(showPulse);
motor[M_RIGHT] = MOTORSPEED;
motor[M_LEFT] = -MOTORSPEED;
while(nMotorEncoder[M_RIGHT] < numDegrees) wait1Msec(5);
motor[M_LEFT] = 0;
motor[M_RIGHT]= 0;
stopCalibration();
nxtDisplayCenteredTextLine(5, "Calibration done");
wait1Msec(5000);
}