Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Bikex.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <sys/time.h>
5 #include <Bikex.h>
6 
10 static int time_diff(struct timeval x , struct timeval y)
11 {
12  int x_ms , y_ms , diff;
13 
14  x_ms = x.tv_sec*1000 + x.tv_usec/1000;
15  y_ms = y.tv_sec*1000 + y.tv_usec/1000;
16 
17  diff = y_ms - x_ms;
18 
19  return diff;
20 }
21 
23 {
24  std::cout << "Creating Bikex" << std::endl;
25 
26  this->unity = new Unity();
27  this->ovr = new Ovr();
28  this->battery = new Battery();
29  this->_break = new Break();
30  this->direction = new Direction();
31  this->oximetry = new Oximetry();
32  this->speed = new Speed();
33 
34  this->currPosition.x = 0.0;
35  this->currPosition.y = 0.0;
36  this->currPosition.z = 0.0;
37  this->currRotation.x = 0.0;
38  this->currRotation.y = 0.0;
39  this->currRotation.z = 0.0;
40 
41  dt = 0;
42  FRAME_RATE = 0;
43  currBattery = 0;
44  currHearBeat = 0;
45  currDistance = 0;
46  currSpeed = 0;
47  currDirection = 0;
48  currAngle = 0;
49 }
50 
52 {
53  std::cout << "Termination Bikex" << std::endl;
54  delete unity;
55  delete ovr;
56  delete battery;
57  delete _break;
58  delete direction;
59  delete oximetry;
60  delete speed;
61 
62  // Termination Devices
64 }
65 
67 {
68  std::cout << "Initiating Bikex" << std::endl;
69 
70  // Ovr initialization
71  ovr->init();
72  ovr->startSensor();
73 
74  // Unity initialization
75  unity->init();
76 
77  // Devices initialization
78  Device::init();
79 }
80 
82 {
83  std::cout << "Bikex state" << std::endl;
84  std::cout << "dt = " << dt << std::endl;
85  std::cout << "FRAME_RATE = " << FRAME_RATE << std::endl;
86  std::cout << "currPosition = [" << currPosition.x << ", " << currPosition.y << ", " << currPosition.z << "]" << std::endl;
87  std::cout << "currRotation = [" << currRotation.x << ", " << currRotation.y << ", " << currRotation.z << "]" << std::endl;
88  std::cout << "currBattery = " << (int)currBattery << std::endl;
89  std::cout << "currHearBeat = " << (int)currHearBeat << std::endl;
90  std::cout << "currDistance = " << (int)currDistance << std::endl;
91  std::cout << "currSpeed = " << (int)currSpeed << std::endl;
92  std::cout << "currDirection = " << (int)currDirection << std::endl;
93  std::cout << "currAngle = " << currAngle << std::endl;
94 }
95 
97 {
98  std::cout << "Calculating player position" << std::endl;
99  // TODO: check how the values are gonna come and check the relation between them
100  // TODO: calculate the change of angle
101  this->currAngle = 90;
102 
103  // TODO: check the case when the player is in a curve: depends on the angle
104  this->currPosition.x = this->currPosition.x + (this->currSpeed * this->dt);
105 
106  // TODO: how are we gonna set the turn ? it also has to come as speed ?
107  this->currPosition.z = this->currPosition.z + (this->currDirection * this->dt);
108 
109  // Note, DO NOT set position.y, it'll be totally responsability of the simulation
110 }
111 
113 {
114  std::cout << "Calculating player rotation" << std::endl;
115  // NOTE: it's probably not like this how we do, again let's check the values
116 }
117 
119 {
120  std::cout << "Setting break intensity" << std::endl;
121  // TODO: do some calculations to set right amount of intensity
122  int altitude = unity->getPlayerAltitude();
123  currPosition.y = (unsigned char)altitude;
124  _break->setData(altitude);
125 }
126 
128 {
129  std::cout << "Writting devices" << std::endl;
130  static char info[256];
131  static int chars_written;
132 
133  // First, common sensors
134  Active::flush();
135 
136  // Now Unity stuff
137  chars_written = sprintf(info, "\rSpeed: %i | Heart: %i | Dist: %i | Batt: %i",
138  this->currSpeed, this->currHearBeat, this->currDistance, this->currBattery);
139  unity->setInfo(info, chars_written);
140  unity->setPlayerPosition(this->currPosition.x, this->currPosition.z);
141  unity->setPlayerRotation(this->currRotation.x, this->currRotation.x, this->currRotation.z);
142 
143  return 0;
144 }
145 
147 {
148  std::cout << "Reading devices" << std::endl;
149  // Make all comon readings
150  Passive::flush();
151  speed->getData(this->currSpeed);
153  oximetry->getData(this->currHearBeat);
154  battery->getData(this->currBattery);
155 
156  // Read from Ovr
157  double garbage;
158  ovr->getXYZW(&this->currRotation.x, &this->currRotation.y, &this->currRotation.z, &garbage);
159 
160  return 0;
161 }
162 
164 {
165 
166  std::cout << "Playing" << std::endl;
167  int samples[SAMPLES_FPS], i_samples = 0;
168  int diff = 0, total = 0, average = 0, i = 0;
169  struct timeval before , after;
170 
171  for (i=0; i<SAMPLES_FPS; i++)
172  samples[i] = 30;
173 
174  while(1)
175  {
176  printf("\rmedia: %d", average);
177 
178  // Begin time count
179  gettimeofday(&before , NULL);
180 
181  // Read all sensors
182  this->readDevices();
183 
184  // Calculate and set player's position and rotation based on sensors readings
185  this->calculatePlayerPosition();
186  this->calculatePlayerRotation();
187  this->setBreakIntensity();
188 
189  // Before rendering frame, write to all devices
190  this->writeDevices();
191 
192  // Finally tells unity it can render the frame
193  // TODO: checks how to make unity wait until next frame rendering
194  unity->render();
195 
196  gettimeofday(&after , NULL);
197 
198  // Time taking task
199  for(i = total = 0; i < SAMPLES_FPS; i++)
200  total += samples[i];
201 
202  diff = time_diff(before , after);
203  samples[i_samples++] = diff;
204  if(i_samples == SAMPLES_FPS)
205  i_samples = 0;
206 
207  average = total/SAMPLES_FPS;
208  if(average < MIN_DT)
209  average = MIN_DT;
210 
211  if(diff < average)
212  usleep((average - diff)*1000);
213  }
214 }
215 
216 float Bikex::calcFPS(int dt)
217 {
218  std::cout << "Calculating FPS" << std::endl;
219  return 1.0;
220 }
void setBreakIntensity()
Definition: Bikex.cpp:118
xyz currPosition
Definition: Bikex.h:39
void calculatePlayerPosition()
Definition: Bikex.cpp:96
int getData(unsigned char &data)
Definition: Battery.cpp:3
static void destroy()
Definition: Device.cpp:57
void render()
Definition: Unity.cpp:195
static int flush()
Definition: Active.cpp:6
void init()
Definition: Unity.cpp:74
int getData(unsigned char &data)
Definition: Speed.cpp:3
#define SAMPLES_FPS
Definition: Bikex.h:14
Bikex()
Definition: Bikex.cpp:22
static void init()
Definition: Device.cpp:51
#define NULL
Battery * battery
Definition: Bikex.h:93
bool startSensor()
Definition: Ovr.cpp:91
Ovr * ovr
Definition: Bikex.h:88
int readDevices()
Definition: Bikex.cpp:146
Definition: Unity.h:15
void play()
Definition: Bikex.cpp:163
double x
Definition: Bikex.h:17
static int time_diff(struct timeval x, struct timeval y)
Definition: Bikex.cpp:10
unsigned char currHearBeat
Definition: Bikex.h:54
Break * _break
Definition: Bikex.h:98
Definition: Speed.h:6
void setPlayerRotation(double x, double y, double z)
Definition: Unity.cpp:161
Oximetry * oximetry
Definition: Bikex.h:108
unsigned char currSpeed
Definition: Bikex.h:64
int currDistance
Definition: Bikex.h:59
int getData(unsigned char &data)
Definition: Direction.cpp:3
unsigned char currBattery
Definition: Bikex.h:49
static int flush()
Definition: Passive.cpp:9
double y
Definition: Bikex.h:17
#define MIN_DT
Definition: Bikex.h:13
Unity * unity
Definition: Bikex.h:83
double z
Definition: Bikex.h:17
float FRAME_RATE
Definition: Bikex.h:34
void printCurrState()
Definition: Bikex.cpp:81
Speed * speed
Definition: Bikex.h:113
bool init()
Definition: Ovr.cpp:39
float currAngle
Definition: Bikex.h:78
~Bikex()
Definition: Bikex.cpp:51
void calculatePlayerRotation()
Definition: Bikex.cpp:112
xyz currRotation
Definition: Bikex.h:44
int getData(unsigned char &data)
Definition: Oximetry.cpp:3
float dt
Definition: Bikex.h:29
void setInfo(const char *info, int chars_written)
Definition: Unity.cpp:178
Definition: Ovr.h:15
Direction * direction
Definition: Bikex.h:103
void getXYZW(double *x, double *y, double *z, double *w)
Definition: Ovr.cpp:106
unsigned char currDirection
Definition: Bikex.h:69
void setPlayerPosition(double x, double z)
Definition: Unity.cpp:144
int writeDevices()
Definition: Bikex.cpp:127
void init()
Definition: Bikex.cpp:66
Definition: Break.h:6
int getPlayerAltitude()
Definition: Unity.cpp:92
int setData(unsigned char data)
Definition: Break.cpp:3
float calcFPS(int dt)
Definition: Bikex.cpp:216
Definition: Battery.h:6