-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhitechnic-compass-test2.c
133 lines (116 loc) · 4.04 KB
/
hitechnic-compass-test2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#pragma config(Sensor, S1, HTCOMPASS, sensorI2CCustom)
#pragma config(Motor, motorB, M_RIGHT, tmotorNormal, PIDControl, reversed)
#pragma config(Motor, motorC, M_LEFT, tmotorNormal, PIDControl, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
* $Id: hitechnic-compass-test2.c 133 2013-03-10 15:15:38Z xander $
*/
/**
* hitechnic-compass.h provides an API for the HiTechnic Compass Sensor. This program
* demonstrates how to use that API to calibrate the compass.
*
* The code here is made to work with the standard NXT Tribot. You will need to edit
* the WHEELDIST, WHEELSIZE and MOTORSPEED figures to make it work with your robot.
*
* Remeber that the robot shouldn't spin more than 360 degrees per 20 seconds. Also
* make sure it spins a bit more than 360, perhaps 1 and 1/4 or 1 and 1/2.
*
* Changelog:
* - 0.1: Initial release
* - 0.2: Added pulsating "*" to indicate progress
* - 0.3: More comments
*
* License: You may use this code as you wish, provided you give credit where it's due.
*
* THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER.
* Xander Soldaat (xander_at_botbench.com)
* 25 November 2009
* version 0.3
*/
#include "drivers/hitechnic-compass.h"
// These measurements are in milimeters.
#define WHEELDIST 115 // distance between the wheels
#define WHEELSIZE 56 // diameter of the wheels
#define MOTORSPEED 4 // speed at which motors should turn
// Lets you know when 20 seconds is over, can help with setting up
// the initial timing and motor speed.
task timeMe() {
wait1Msec(20000);
PlaySound(soundBeepBeep);
while(bSoundActive) EndTimeSlice();
}
// Pulse a big "*" at the bottom of the screen to show that it's
// doing something.
task showPulse() {
while (true) {
nxtDisplayCenteredBigTextLine(6, " ");
wait1Msec(400);
nxtDisplayCenteredBigTextLine(6, "*");
wait1Msec(400);
}
}
// Does some voodoo maths to calculate how many times the wheels should rotate
// to make the robot spin about 360 degrees.
int numRotations() {
return ((WHEELDIST * 3142) / 1000) / ((WHEELSIZE * 3142) / 1000);
}
// Start the calibration and complain loudly if something goes wrong
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();
}
}
// Stop the calibration and complain loudly if somethign goes wrong
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;
// This will make the robot spin about 1.5 times, depends on many factors, YYMV, etc
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);
}
/*
* $Id: hitechnic-compass-test2.c 133 2013-03-10 15:15:38Z xander $
*/