Merge pull request #471 from PhilipRosedale/master

Detect and stop moving on transmitter signal lost
This commit is contained in:
ZappoMan 2013-05-30 16:46:58 -07:00
commit 4583f38352
4 changed files with 27 additions and 3 deletions

View file

@ -877,6 +877,8 @@ void Application::idle() {
_serialPort.readData(deltaTime);
}
// Update transmitter
// Sample hardware, update view frustum if needed, and send avatar data to mixer/agents
updateAvatar(deltaTime);

View file

@ -278,6 +278,7 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) {
// Add thrusts from Transmitter
if (transmitter) {
transmitter->checkForLostTransmitter();
glm::vec3 rotation = transmitter->getEstimatedRotation();
const float TRANSMITTER_MIN_RATE = 1.f;
const float TRANSMITTER_MIN_YAW_RATE = 4.f;

View file

@ -20,11 +20,25 @@ Transmitter::Transmitter() :
_isConnected(false),
_lastRotationRate(0,0,0),
_lastAcceleration(0,0,0),
_estimatedRotation(0,0,0)
_estimatedRotation(0,0,0),
_lastReceivedPacket(NULL)
{
}
void Transmitter::checkForLostTransmitter() {
// If we are in motion, check for loss of transmitter packets
if (glm::length(_estimatedRotation) > 0.f) {
timeval now;
gettimeofday(&now, NULL);
const int TIME_TO_ASSUME_LOST_MSECS = 2000;
int msecsSinceLast = diffclock(_lastReceivedPacket, &now);
if (msecsSinceLast > TIME_TO_ASSUME_LOST_MSECS) {
resetLevels();
printLog("Transmitter signal lost.\n");
}
}
}
void Transmitter::resetLevels() {
_lastRotationRate *= 0.f;
_estimatedRotation *= 0.f;
@ -34,6 +48,11 @@ void Transmitter::processIncomingData(unsigned char* packetData, int numBytes) {
const int PACKET_HEADER_SIZE = 1; // Packet's first byte is 'T'
const int ROTATION_MARKER_SIZE = 1; // 'R' = Rotation (clockwise about x,y,z)
const int ACCELERATION_MARKER_SIZE = 1; // 'A' = Acceleration (x,y,z)
if (!_lastReceivedPacket) {
_lastReceivedPacket = new timeval;
}
gettimeofday(_lastReceivedPacket, NULL);
if (numBytes == PACKET_HEADER_SIZE + ROTATION_MARKER_SIZE + ACCELERATION_MARKER_SIZE
+ sizeof(_lastRotationRate) + sizeof(_lastAcceleration)
+ sizeof(_touchState.x) + sizeof(_touchState.y) + sizeof(_touchState.state)) {
@ -69,12 +88,12 @@ void Transmitter::processIncomingData(unsigned char* packetData, int numBytes) {
_estimatedRotation.y *= (1.f - DECAY_RATE * DELTA_TIME);
if (!_isConnected) {
printf("Transmitter V2 Connected.\n");
printLog("Transmitter Connected.\n");
_isConnected = true;
_estimatedRotation *= 0.0;
}
} else {
printf("Transmitter V2 packet read error, %d bytes.\n", numBytes);
printLog("Transmitter packet read error, %d bytes.\n", numBytes);
}
}

View file

@ -26,6 +26,7 @@ class Transmitter
public:
Transmitter();
void render();
void checkForLostTransmitter();
void resetLevels();
void renderLevels(int width, int height);
bool isConnected() { return _isConnected; };
@ -41,6 +42,7 @@ private:
glm::vec3 _lastAcceleration;
glm::vec3 _estimatedRotation;
TouchState _touchState;
timeval* _lastReceivedPacket;
#endif /* defined(__hifi__Transmitter__) */
};