mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 15:09:24 +02:00
initial setup of polling for newly connected headset
This commit is contained in:
parent
50f1ba8c2d
commit
d0203158c8
3 changed files with 94 additions and 51 deletions
|
@ -15,6 +15,12 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "SerialInterface.h"
|
#include "SerialInterface.h"
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#include <regex.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
int serial_fd;
|
int serial_fd;
|
||||||
const int MAX_BUFFER = 255;
|
const int MAX_BUFFER = 255;
|
||||||
|
@ -23,12 +29,48 @@ int serial_buffer_pos = 0;
|
||||||
int samples_total = 0;
|
int samples_total = 0;
|
||||||
|
|
||||||
const int ZERO_OFFSET = 2048;
|
const int ZERO_OFFSET = 2048;
|
||||||
|
const short NO_READ_MAXIMUM = 10;
|
||||||
|
|
||||||
|
SerialInterface::SerialInterface() {
|
||||||
|
active = false;
|
||||||
|
noReadCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SerialInterface::pair() {
|
||||||
|
// look for a matching gyro setup
|
||||||
|
DIR *devDir;
|
||||||
|
struct dirent *entry;
|
||||||
|
int matchStatus;
|
||||||
|
regex_t regex;
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
// for now this only works on OS X, where the usb serial shows up as /dev/tty.usb*
|
||||||
|
if((devDir = opendir("/dev"))) {
|
||||||
|
while((entry = readdir(devDir))) {
|
||||||
|
regcomp(®ex, "tty\\.usb", REG_EXTENDED|REG_NOSUB);
|
||||||
|
matchStatus = regexec(®ex, entry->d_name, (size_t) 0, NULL, 0);
|
||||||
|
if (matchStatus == 0) {
|
||||||
|
char *serialPortname = new char[100];
|
||||||
|
sprintf(serialPortname, "/dev/%s", entry->d_name);
|
||||||
|
|
||||||
|
init(serialPortname, 115200);
|
||||||
|
|
||||||
|
delete [] serialPortname;
|
||||||
|
}
|
||||||
|
regfree(®ex);
|
||||||
|
}
|
||||||
|
closedir(devDir);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// Init the serial port to the specified values
|
// Init the serial port to the specified values
|
||||||
int SerialInterface::init(char * portname, int baud)
|
int SerialInterface::init(char* portname, int baud)
|
||||||
{
|
{
|
||||||
serial_fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
|
serial_fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||||
|
|
||||||
|
printf("Attemping to open serial interface: %s\n", portname);
|
||||||
|
|
||||||
if (serial_fd == -1) return -1; // Failed to open port
|
if (serial_fd == -1) return -1; // Failed to open port
|
||||||
|
|
||||||
struct termios options;
|
struct termios options;
|
||||||
|
@ -69,8 +111,11 @@ int SerialInterface::init(char * portname, int baud)
|
||||||
serial_buffer[i] = ' ';
|
serial_buffer[i] = ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("Serial interface opened!\n");
|
||||||
|
|
||||||
return 0; // Init serial port was a success
|
active = true;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset Trailing averages to the current measurement
|
// Reset Trailing averages to the current measurement
|
||||||
|
@ -133,8 +178,12 @@ void SerialInterface::readData() {
|
||||||
const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01};
|
const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01};
|
||||||
char bufchar[1];
|
char bufchar[1];
|
||||||
|
|
||||||
|
bool atLeastOneRead = false;
|
||||||
|
|
||||||
while (read(serial_fd, &bufchar, 1) > 0)
|
while (read(serial_fd, &bufchar, 1) > 0)
|
||||||
{
|
{
|
||||||
|
atLeastOneRead = true;
|
||||||
|
|
||||||
//std::cout << bufchar[0];
|
//std::cout << bufchar[0];
|
||||||
serial_buffer[serial_buffer_pos] = bufchar[0];
|
serial_buffer[serial_buffer_pos] = bufchar[0];
|
||||||
serial_buffer_pos++;
|
serial_buffer_pos++;
|
||||||
|
@ -169,6 +218,28 @@ void SerialInterface::readData() {
|
||||||
serial_buffer_pos = 0;
|
serial_buffer_pos = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!atLeastOneRead) {
|
||||||
|
noReadCount++;
|
||||||
|
std::cout << "#" << noReadCount << " blank read from serial.\n";
|
||||||
|
if (noReadCount >= NO_READ_MAXIMUM) {
|
||||||
|
disconnectSerial();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SerialInterface::disconnectSerial() {
|
||||||
|
std::cout << "Reached maximum blank read count. Shutting down serial.\n";
|
||||||
|
|
||||||
|
active = false;
|
||||||
|
noReadCount = 0;
|
||||||
|
|
||||||
|
// Clear the measured and average channel data
|
||||||
|
for (int i = 1; i < NUM_CHANNELS; i++)
|
||||||
|
{
|
||||||
|
lastMeasured[i] = 0;
|
||||||
|
trailingAverage[i] = 0.0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,8 @@
|
||||||
|
|
||||||
class SerialInterface {
|
class SerialInterface {
|
||||||
public:
|
public:
|
||||||
int init(char * portname, int baud);
|
SerialInterface();
|
||||||
|
void pair();
|
||||||
void readData();
|
void readData();
|
||||||
int getLED() {return LED;};
|
int getLED() {return LED;};
|
||||||
int getNumSamples() {return samplesAveraged;};
|
int getNumSamples() {return samplesAveraged;};
|
||||||
|
@ -40,11 +41,15 @@ public:
|
||||||
float getTrailingValue(int num) {return trailingAverage[num];};
|
float getTrailingValue(int num) {return trailingAverage[num];};
|
||||||
void resetTrailingAverages();
|
void resetTrailingAverages();
|
||||||
void renderLevels(int width, int height);
|
void renderLevels(int width, int height);
|
||||||
|
bool active;
|
||||||
private:
|
private:
|
||||||
|
int init(char * portname, int baud);
|
||||||
|
void disconnectSerial();
|
||||||
int lastMeasured[NUM_CHANNELS];
|
int lastMeasured[NUM_CHANNELS];
|
||||||
float trailingAverage[NUM_CHANNELS];
|
float trailingAverage[NUM_CHANNELS];
|
||||||
int samplesAveraged;
|
int samplesAveraged;
|
||||||
int LED;
|
int LED;
|
||||||
|
int noReadCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -113,7 +113,6 @@ Field field;
|
||||||
Audio audio(&myHead, &audioScope);
|
Audio audio(&myHead, &audioScope);
|
||||||
|
|
||||||
#define RENDER_FRAME_MSECS 8
|
#define RENDER_FRAME_MSECS 8
|
||||||
#define SLEEP 0
|
|
||||||
int steps_per_frame = 0;
|
int steps_per_frame = 0;
|
||||||
|
|
||||||
float yaw =0.f; // The yaw, pitch for the avatar head
|
float yaw =0.f; // The yaw, pitch for the avatar head
|
||||||
|
@ -161,9 +160,7 @@ int speed;
|
||||||
//
|
//
|
||||||
|
|
||||||
SerialInterface serialPort;
|
SerialInterface serialPort;
|
||||||
char serial_portname[] = "/dev/tty.usbmodem411";
|
|
||||||
|
|
||||||
int serial_on = 0;
|
|
||||||
int latency_display = 1;
|
int latency_display = 1;
|
||||||
//int adc_channels[NUM_CHANNELS];
|
//int adc_channels[NUM_CHANNELS];
|
||||||
//float avg_adc_channels[NUM_CHANNELS];
|
//float avg_adc_channels[NUM_CHANNELS];
|
||||||
|
@ -253,10 +250,10 @@ void Timer(int extra)
|
||||||
printf("packet test = %4.1f\n", sendTime);
|
printf("packet test = %4.1f\n", sendTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we haven't detected gyros, check for them now
|
||||||
// Send a message to ourselves
|
if (!serialPort.active) {
|
||||||
//char test[]="T";
|
serialPort.pair();
|
||||||
//agentSocket.send("127.0.0.1", AGENT_UDP_PORT, test, 1);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void display_stats(void)
|
void display_stats(void)
|
||||||
|
@ -269,7 +266,7 @@ void display_stats(void)
|
||||||
sprintf(stats, "FPS = %3.0f Pkts/s = %d Bytes/s = %d ",
|
sprintf(stats, "FPS = %3.0f Pkts/s = %d Bytes/s = %d ",
|
||||||
FPS, packets_per_second, bytes_per_second);
|
FPS, packets_per_second, bytes_per_second);
|
||||||
drawtext(10, 30, 0.10, 0, 1.0, 0, stats);
|
drawtext(10, 30, 0.10, 0, 1.0, 0, stats);
|
||||||
if (serial_on) {
|
if (serialPort.active) {
|
||||||
sprintf(stats, "ADC samples = %d, LED = %d",
|
sprintf(stats, "ADC samples = %d, LED = %d",
|
||||||
serialPort.getNumSamples(), serialPort.getLED());
|
serialPort.getNumSamples(), serialPort.getLED());
|
||||||
drawtext(300, 30, 0.10, 0, 1.0, 0, stats);
|
drawtext(300, 30, 0.10, 0, 1.0, 0, stats);
|
||||||
|
@ -329,26 +326,7 @@ void init(void)
|
||||||
myHead.setNoise(noise);
|
myHead.setNoise(noise);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (serial_on)
|
|
||||||
{
|
|
||||||
// Call readsensors for a while to get stable initial values on sensors
|
|
||||||
printf( "Stabilizing sensors... " );
|
|
||||||
gettimeofday(&timer_start, NULL);
|
|
||||||
int done = 0;
|
|
||||||
while (!done)
|
|
||||||
{
|
|
||||||
serialPort.readData();
|
|
||||||
gettimeofday(&timer_end, NULL);
|
|
||||||
if (diffclock(&timer_start, &timer_end) > 1000) done = 1;
|
|
||||||
}
|
|
||||||
gravity.x = serialPort.getValue(ACCEL_X);
|
|
||||||
gravity.y = serialPort.getValue(ACCEL_Y);
|
|
||||||
gravity.z = serialPort.getValue(ACCEL_Z);
|
|
||||||
|
|
||||||
std::cout << "Gravity: " << gravity.x << "," << gravity.y << "," << gravity.z << "\n";
|
|
||||||
printf( "Done.\n" );
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef MARKER_CAPTURE
|
#ifdef MARKER_CAPTURE
|
||||||
if(marker_capture_enabled){
|
if(marker_capture_enabled){
|
||||||
|
@ -396,7 +374,10 @@ void reset_sensors()
|
||||||
|
|
||||||
myHead.reset();
|
myHead.reset();
|
||||||
myHand.reset();
|
myHand.reset();
|
||||||
if (serial_on) serialPort.resetTrailingAverages();
|
|
||||||
|
if (serialPort.active) {
|
||||||
|
serialPort.resetTrailingAverages();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_pos(float frametime)
|
void update_pos(float frametime)
|
||||||
|
@ -855,10 +836,8 @@ void idle(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read serial data
|
// Read serial data
|
||||||
if (serial_on) serialPort.readData();
|
if (serialPort.active) {
|
||||||
if (SLEEP)
|
serialPort.readData();
|
||||||
{
|
|
||||||
usleep(SLEEP);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -985,18 +964,6 @@ int main(int argc, char** argv)
|
||||||
printf("Stdev=FAIL ");
|
printf("Stdev=FAIL ");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
//
|
|
||||||
// Try to connect the serial port I/O
|
|
||||||
//
|
|
||||||
if(serialPort.init(serial_portname, 115200) == -1) {
|
|
||||||
printf("Unable to open serial port: %s\n", serial_portname);
|
|
||||||
serial_on = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("Serial Port Initialized\n");
|
|
||||||
serial_on = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// create thread for receipt of data via UDP
|
// create thread for receipt of data via UDP
|
||||||
pthread_t networkReceiveThread;
|
pthread_t networkReceiveThread;
|
||||||
|
|
Loading…
Reference in a new issue