-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGyroSensor.java
More file actions
96 lines (82 loc) · 1.78 KB
/
Copy pathGyroSensor.java
File metadata and controls
96 lines (82 loc) · 1.78 KB
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
package ev3.exercises.library;
import lejos.hardware.port.Port;
import lejos.hardware.sensor.EV3GyroSensor;
import lejos.robotics.Gyroscope;
import lejos.robotics.SampleProvider;
public class GyroSensor implements Gyroscope
{
EV3GyroSensor sensor;
SampleProvider sp;
float [] sample;
int offset = 0;
/**
* Creates GyroSensor object. This is a wrapper class for EV3GyroSensor.
* @param port SensorPort of EV3GyroSensor device.
*/
public GyroSensor(Port port)
{
sensor = new EV3GyroSensor(port);
sp = sensor.getAngleAndRateMode();
sample = new float[sp.sampleSize()];
sensor.reset();
}
/**
* Returns the underlying EV3GryoSensor object.
* @return Sensor object reference.
*/
public EV3GyroSensor getSensor()
{
return sensor;
}
/**
* Return the current angular velocity from the gyro.
* @return The angular velocity in degrees/second. Negative
* if turning right.
*/
public float getAngularVelocity()
{
sp.fetchSample(sample, 0);
return sample[1];
}
/**
* Return the current accumulated angle from starting point from the gyro.
* @return The accumulated angle in degrees. Negative if turning right past zero.
*/
public int getAngle()
{
sp.fetchSample(sample, 0);
return (int) sample[0] - offset;
}
/**
* Reset angle to zero.
*/
public void reset()
{
sp.fetchSample(sample, 0);
offset = (int) sample[0];
}
/**
* Recalibrate gyro and reset gyro angle to zero.
* May take several seconds to complete.
*/
public void resetGyro()
{
sensor.reset();
offset = 0;
}
/**
* Release resources.
*/
public void close()
{
sensor.close();
}
/**
* Same as ResetGyro().
*/
@Override
public void recalibrateOffset()
{
resetGyro();
}
}