Moderators: adafruit_support_bill, adafruit
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime> // USED BY RAND
#include <windows.h> // Used for delay
using namespace std;
#define SAMPLE_RATE 0.020 // Sample rate in Milliseconds
#define GRAVITYFT_SEC 32 // Gravity velocity 32 feet/sec
#define INCH_FOOT 12 // 12 inches in foot, from velocity to inch displacement calculation
int main(int argc, char *argv[])
{
srand((unsigned)time(0)); // SEED RAND() for simulation of Geforce Readings
// SIMULATING ACCELERATION READINGS INTO A CIRCULAR BUFFER
// circular_buffer Acceleration; // Create a new Circular buffer for Acceleration
// cb_init(&Acceleration, 150, 4); // Sampling @ 50HZ, 3 seconds of data = 150, size is float data of 4 bytes
//Simulate a sample run of Acceleration data using Rand()
// WE WILL BE SIMULATING "RANDOM" GEFORCE RATINGS using the rand() function constraining to -1.6 to 1.6 GFORCE
// These ratings are consistent with our road tests of apparently random vibration and Geforce readings not exceeding about 1.6 G's
float Gforce[150]; // Random Geforce for 3 second window of data
float velocity[150]; // Hold velocity information
float displacement[150]; // Hold Displacement information
float LO = -1.6; // Low GForce limit recorded from 6 road tests at different speeds
float HI = 1.6; // High GForce limit recorded from 6 road tests at different speeds
for(int i = 0; i < 150; i++) // 3 Second iwndow of random acceleration data
{
Gforce[i] = LO + (float)rand()/((float)RAND_MAX/(HI-LO)); // Borrowed from Stackexchange : http://stackoverflow.com/questions/686353/c-random-float
if( i == 0) // Initial values @ first Acceleration
{
velocity[i] = Gforce[i] * SAMPLE_RATE * GRAVITYFT_SEC; // Initial velocity
displacement[i] = velocity[i] * SAMPLE_RATE * INCH_FOOT; // Initial Displacement
}
else
{
velocity[i] = velocity[0] + (Gforce[i] * SAMPLE_RATE * GRAVITYFT_SEC); // Calculate running velocity into buffer
displacement[i] = displacement[0] +(velocity[i] * SAMPLE_RATE * INCH_FOOT); // Calculate running displacement into buffer
}
//cout << endl << Gforce[i]; // Debugging
//cb_push_back(&Acceleration, &Gforce[i]); // Push the GeForce into the circular buffer
Sleep(SAMPLE_RATE*1000); // 20mS delay simulates 50HZ sampling rate Sleep() expects number in mS already so * 1000
}
// PRINT RESULTS
for (int j = 0; j < 150; j++)
{
cout << setprecision (3) << Gforce[j] << "\t\t" << velocity[j] << "\t\t" << displacement[j] << endl;
}
// READ THE BUFFER
//cb_free(&Acceleration); // Pervent Memory leaks
system("PAUSE");
return EXIT_SUCCESS;
}
GFORCE FT/SEC Inch Displacement Z axis
-0.93 -0.595 -0.143
-0.377 -0.836 -0.344
0.343 -0.375 -0.233
1.16 0.148 -0.107
-0.559 -0.953 -0.371
0.601 -0.211 -0.193
0.124 -0.516 -0.267
-1.27 -1.41 -0.48
0.809 -0.0775 -0.161
-1.06 -1.27 -0.448
-1.16 -1.34 -0.463
-1.1 -1.3 -0.455
-1.33 -1.44 -0.489
0.256 -0.431 -0.246
-1.44 -1.52 -0.507
1.26 0.211 -0.0923
0.932 0.0015 -0.142
0.789 -0.0903 -0.164
0.318 -0.392 -0.237
-0.355 -0.822 -0.34
1.49 0.357 -0.057
1.38 0.289 -0.0733
1.13 0.131 -0.111
-0.342 -0.814 -0.338
-1.52 -1.57 -0.519
-0.406 -0.855 -0.348
-1.22 -1.37 -0.473
-0.182 -0.712 -0.314
0.0257 -0.579 -0.282
0.112 -0.523 -0.268
-0.624 -0.994 -0.381
-0.35 -0.819 -0.339
0.323 -0.388 -0.236
-1.26 -1.4 -0.479
-1.4 -1.49 -0.501
0.409 -0.333 -0.223
0.328 -0.385 -0.235
-0.713 -1.05 -0.395
-1.33 -1.45 -0.49
0.926 -0.00219 -0.143
1.39 0.293 -0.0725
-0.613 -0.987 -0.38
0.0183 -0.583 -0.283
-0.909 -1.18 -0.425
1.19 0.17 -0.102
1.47 0.345 -0.0601
0.844 -0.0549 -0.156
1.46 0.342 -0.0607
0.555 -0.24 -0.2
0.851 -0.0502 -0.155
1.19 0.164 -0.103
0.324 -0.387 -0.236
-0.951 -1.2 -0.432
0.75 -0.115 -0.17
-0.0729 -0.642 -0.297
0.641 -0.185 -0.187
0.909 -0.0133 -0.146
-0.149 -0.69 -0.308
0.461 -0.3 -0.215
-1.32 -1.44 -0.488
-0.402 -0.852 -0.347
-1.03 -1.25 -0.443
0.711 -0.14 -0.176
1.14 0.137 -0.11
0.797 -0.0849 -0.163
1.31 0.241 -0.085
-0.795 -1.1 -0.408
-0.658 -1.02 -0.387
1.1 0.107 -0.117
1.54 0.388 -0.0497
-0.688 -1.04 -0.391
0.0859 -0.54 -0.272
1.3 0.234 -0.0865
-0.853 -1.14 -0.417
-1.47 -1.53 -0.511
-0.973 -1.22 -0.435
0.409 -0.333 -0.223
-0.793 -1.1 -0.407
-0.664 -1.02 -0.388
1.31 0.247 -0.0836
1.27 0.22 -0.09
0.733 -0.126 -0.173
-0.324 -0.803 -0.335
0.894 -0.0229 -0.148
-0.528 -0.933 -0.367
-0.0603 -0.634 -0.295
-0.363 -0.828 -0.341
0.928 -0.00119 -0.143
-0.749 -1.07 -0.401
-1.14 -1.32 -0.46
1.55 0.399 -0.0469
-0.544 -0.943 -0.369
-0.644 -1.01 -0.384
-1.32 -1.44 -0.488
-1.52 -1.57 -0.519
0.383 -0.35 -0.227
-1.13 -1.32 -0.46
0.191 -0.473 -0.256
-1.27 -1.41 -0.481
-0.505 -0.918 -0.363
1.13 0.129 -0.112
0.39 -0.345 -0.226
1.32 0.248 -0.0832
-0.787 -1.1 -0.406
-1.27 -1.41 -0.48
1.23 0.191 -0.097
-1.04 -1.26 -0.445
-1.43 -1.51 -0.505
0.726 -0.13 -0.174
0.241 -0.441 -0.249
0.123 -0.516 -0.267
-0.663 -1.02 -0.387
-1.29 -1.42 -0.484
1.57 0.408 -0.0448
-0.408 -0.856 -0.348
0.341 -0.376 -0.233
0.191 -0.473 -0.256
1.05 0.0739 -0.125
1.18 0.16 -0.104
-1.43 -1.51 -0.505
0.157 -0.494 -0.261
-0.109 -0.665 -0.302
-0.511 -0.922 -0.364
1.06 0.0844 -0.123
0.161 -0.492 -0.261
0.659 -0.174 -0.184
-1.17 -1.34 -0.465
-1.06 -1.27 -0.448
0.201 -0.466 -0.255
1.58 0.417 -0.0426
-1.39 -1.48 -0.499
0.509 -0.269 -0.207
-0.881 -1.16 -0.421
0.595 -0.214 -0.194
0.852 -0.0494 -0.155
1.01 0.0509 -0.131
-0.804 -1.11 -0.409
-0.462 -0.891 -0.357
1.18 0.163 -0.104
-1.11 -1.31 -0.456
-0.542 -0.942 -0.369
0.64 -0.185 -0.187
-0.204 -0.725 -0.317
0.155 -0.496 -0.262
-1.01 -1.24 -0.441
-1.41 -1.5 -0.503
0.0378 -0.571 -0.28
-1.04 -1.26 -0.446
1.14 0.136 -0.11
-1.34 -1.45 -0.491
Press any key to continue . . .
GFORCE FT/SEC Inch Displacement Z axis
-0.882 -0.565 -0.136
0.199 -0.437 -0.24
-1.32 -1.29 -0.549
0.928 -0.691 -0.715
0.6 -0.307 -0.788
1.47 0.635 -0.636
0.849 1.18 -0.353
-0.247 1.02 -0.108
1.29 1.85 0.335
0.298 2.04 0.824
-1.04 1.37 1.15
1.1 2.08 1.65
1.52 3.05 2.38
0.078 3.1 3.12
-0.0125 3.09 3.87
1.24 3.88 4.8
0.845 4.42 5.86
0.25 4.58 6.96
0.0463 4.61 8.06
1.37 5.49 9.38
-0.15 5.39 10.7
0.947 6 12.1
1.18 6.75 13.7
-0.791 6.25 15.2
-1.43 5.33 16.5
-1.58 4.32 17.5
1.52 5.29 18.8
-0.208 5.16 20.1
1.36 6.03 21.5
-0.294 5.84 22.9
1.22 6.62 24.5
1.14 7.35 26.3
1.01 8 28.2
0.284 8.18 30.1
1.18 8.93 32.3
-1.43 8.02 34.2
-0.167 7.91 36.1
1.14 8.64 38.2
-1.4 7.74 40
-1.49 6.79 41.7
-0.926 6.2 43.2
-0.575 5.83 44.6
0.978 6.46 46.1
-0.909 5.87 47.5
1.46 6.81 49.2
0.353 7.04 50.8
-1.12 6.32 52.4
-1.12 5.6 53.7
-0.141 5.51 55
0.463 5.8 56.4
-1.1 5.1 57.6
0.591 5.48 59
0.0912 5.54 60.3
-0.47 5.23 61.5
-0.437 4.96 62.7
0.734 5.42 64
-0.343 5.21 65.3
0.836 5.74 66.7
-1.11 5.03 67.9
-0.771 4.54 69
-0.783 4.04 69.9
-0.501 3.72 70.8
-0.569 3.35 71.6
0.765 3.84 72.5
0.568 4.21 73.5
-1.45 3.28 74.3
0.391 3.53 75.2
0.339 3.75 76.1
0.797 4.26 77.1
1.3 5.09 78.3
0.237 5.24 79.6
1.52 6.21 81.1
0.314 6.41 82.6
0.369 6.65 84.2
-0.598 6.26 85.7
-0.905 5.68 87.1
-0.732 5.22 88.3
-1.47 4.27 89.4
0.828 4.8 90.5
0.261 4.97 91.7
0.0473 5 92.9
1.53 5.98 94.3
1.24 6.77 96
-0.0228 6.76 97.6
-0.0453 6.73 99.2
-1.07 6.04 101
-0.345 5.82 102
0.652 6.24 104
1.37 7.12 105
1.15 7.85 107
0.0238 7.87 109
1.43 8.79 111
1.08 9.48 113
1.53 10.5 116
-0.709 10 118
-0.811 9.48 121
-1.06 8.8 123
-1.22 8.02 125
-1.4 7.13 126
0.129 7.21 128
0.199 7.34 130
-0.182 7.22 132
0.135 7.31 133
0.885 7.87 135
0.678 8.31 137
0.922 8.9 139
-1.54 7.91 141
-1.16 7.16 143
-0.632 6.76 145
1.3 7.59 146
-0.67 7.16 148
0.124 7.24 150
-1.19 6.48 151
-0.728 6.01 153
1.22 6.79 154
-1.33 5.94 156
-0.402 5.69 157
-0.532 5.35 159
1.27 6.16 160
0.323 6.37 162
0.428 6.64 163
0.414 6.91 165
-0.614 6.51 166
1.37 7.39 168
0.449 7.68 170
0.55 8.03 172
1.33 8.88 174
-1.2 8.11 176
-0.641 7.7 178
-1.59 6.69 179
1.02 7.34 181
-0.86 6.79 183
-1.55 5.79 184
-0.515 5.46 189
0.352 5.69 187
0.824 6.22 188
1.14 6.94 190
-1.03 6.29 192
-1.13 5.56 193
0.139 5.65 194
0.293 5.84 196
1.08 6.53 197
-1.23 5.75 199
-1.1 5.04 200
-1.17 4.29 201
-0.8 3.78 202
-0.905 3.2 203
-0.0769 3.15 203
-0.323 2.95 204
-0.0189 2.93 205
Press any key to continue . . .#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime> // USED BY RAND
#include <windows.h> // Used for delay
using namespace std;
#define SAMPLE_RATE 0.020 // Sample rate in Milliseconds
#define GRAVITYFT_SEC 32 // Gravity velocity 32 feet/sec
#define INCH_FOOT 12 // 12 inches in foot, from velocity to inch displacement calculation
void READ_BUFFER();
class RingBuffer
{
private:
int size;
float* buffer;
int start;
int end;
int cnt;
public:
RingBuffer(int size);
~RingBuffer();
void push(float value);
float pop();
float peek();
int count();
};
RingBuffer::RingBuffer(int size)
: size(size), start(0), end(0), cnt(0)
{
buffer = (float*)calloc(size, sizeof(float));
}
RingBuffer::~RingBuffer()
{
free(buffer);
}
void RingBuffer::push(float value)
{
buffer[end] = value;
if (++end > size) end = 0;
if (cnt == size) {
if (++start > size) start = 0;
} else {
++cnt;
}
}
float RingBuffer::pop()
{
float value = buffer[start];
if (cnt > 0) {
--cnt;
if (++start > size) start = 0;
}
return value;
}
float RingBuffer::peek()
{
return buffer[start];
}
int RingBuffer::count()
{
return cnt;
}
RingBuffer Gforce(150);
RingBuffer velocity(150);
RingBuffer displacement(150);
int main(int argc, char *argv[])
{
srand((unsigned)time(0)); // SEED RAND() for simulation of Geforce Readings
// SIMULATING ACCELERATION READINGS INTO A CIRCULAR BUFFER
//Simulate a sample run of Acceleration data using Rand()
// WE WILL BE SIMULATING "RANDOM" GEFORCE RATINGS using the rand() function constraining to -1.6 to 1.6 GFORCE
// These ratings are consistent with our road tests of apparently random vibration and Geforce readings not exceeding about 1.6 G's
//float Gforce[150]; // Random Geforce for 3 second window of data
//float velocity[150]; // Hold velocity information
// float displacement[150]; // Hold Displacement information
float LO = -1.6; // Low GForce limit recorded from 6 road tests at different speeds
float HI = 1.6; // High GForce limit recorded from 6 road tests at different speeds
for(int i = 0; i < 300; i++) // 6 Second iwndow of random acceleration data pretend this is streaming data from analogREAD()
{
if (Gforce.count() == 150) // if we are at a 3 second window
READ_BUFFER(); // READ the contents
Gforce.push(LO + (float)rand()/((float)RAND_MAX/(HI-LO))); // Borrowed from Stackexchange : http://stackoverflow.com/questions/686353/c-random-float
/*
if( i == 0) // Initial values @ first Acceleration
{
velocity.push((Gforce.peek() * SAMPLE_RATE * GRAVITYFT_SEC)); // Initial velocity
displacement.push((velocity.peek() * SAMPLE_RATE * INCH_FOOT)); // Initial Displacement
}
*/
velocity.push(velocity.peek() + (Gforce.pop() * SAMPLE_RATE * GRAVITYFT_SEC)); // Calculate running velocity into buffer
displacement.push(displacement.peek() +(velocity.pop() * SAMPLE_RATE * INCH_FOOT)); // Calculate running displacement into buffer
Sleep(SAMPLE_RATE*1000); // 20mS delay simulates 50HZ sampling rate Sleep() expects number in mS already so * 1000
}
system("PAUSE");
return EXIT_SUCCESS;
}
void READ_BUFFER()
{
// PRINT RESULTS
cout << "GFORCE" << "\t\t" << "FT/SEC" << "\t\t" << "Inch Displacement Z axis" << endl << endl;
for (int j = 0; j < 150; j++)
{
cout << setprecision (3) << Gforce.pop() << "\t\t" << velocity.pop() << "\t\t" << displacement.pop() << endl;
}
}
// Pothole Detector V1.0 by Arctic_Eddie, 2/5/2013
// This sketch detects and processes data collected from an accelerometer for a fixed length of time
// Define constants
#define SAMPLE_DELAY 200 // Sample delay in milliseconds, probably 20
#define GRAVITYFT_SEC 32.174 // Gravity to velocity,feet/sec
#define G_OFFSET 2.0 // Make the G reading zero for the resting state
#define G_SCALE 4.0 // If accelerometer is on +/- 2G scale
#define INCH_FOOT 12.0 // 12 inches in foot, from velocity to inch displacement calculation
#define NUM_SAMPLES 20 // Capture 6 seconds, probably 300
#define ACCEL_PIN A0 // Use analog pin A0
// Define global veriables
uint16_t indeX = 0; // Pointer into data arrays
uint32_t dTime; // Clock time for delayed reading
float acceleration[NUM_SAMPLES]; // Space for accel samples
float velocity[NUM_SAMPLES]; // Space for velocity samples
float displacement[NUM_SAMPLES]; // Space for displacement samples
// Set up system
void setup()
{
Serial.begin( 115200 ); // Prepare the serial monitor to receive the data
Serial.println( "\n\rTime, Acceleration, Velocity, Displacement" ); // Print heading
// Initialize the accelerometer ****************************
// Start the delay clock
dTime = millis();
}
void loop()
{
// Collect the specified amount of data, calculate other values, and print results
// This section is for a single pass to fill the arrays once ********************
// Get a random value or use accelerometer reading here
acceleration[indeX] = ( ( analogRead( ACCEL_PIN ) / 1024.0 ) * G_SCALE ) - G_OFFSET; // Get an acceleration reading
// Calculate the running sum integral for velocity and displacement
if( indeX == 0 ) // First reading, no previous data to add to running sum
{
velocity[indeX] = acceleration[indeX] * SAMPLE_DELAY * GRAVITYFT_SEC / 1000.0;
displacement[indeX] = velocity[indeX] * SAMPLE_DELAY * INCH_FOOT / 1000.0;
}
else // Not the first reading so add the data from the previous index
{
velocity[indeX] = ( acceleration[indeX] * SAMPLE_DELAY * GRAVITYFT_SEC / 1000.0 )
+ velocity[indeX-1]; // The running sum
displacement[indeX] = ( velocity[indeX] * SAMPLE_DELAY * INCH_FOOT / 1000.0 )
+ displacement[indeX-1]; // The running sum
}
// Print the results in spreadsheet readable format
Serial.print( float( indeX * SAMPLE_DELAY ) / 1000.0 ); // The actual time value
Serial.print( ", " ); // Add a comma separator
Serial.print( acceleration[indeX] ); // Acceleration
Serial.print( ", " ); // Add a comma separator
Serial.print( velocity[indeX] ); // Velocity
Serial.print( ", " ); // Add a comma separator
Serial.println( displacement[indeX] ); // Displacement
// Pause until the next interval
// Check clock for correct delay
while( ( millis() - dTime ) < ( SAMPLE_DELAY * indeX ) ) {} // Pause here until sample interval has passed
indeX++;
if( indeX > NUM_SAMPLES ) // Stall here, test is over
{
Serial.print( "End of test" );
while( true ) {} // Stall here
}
}
// Supporting routines go here *********************************************
Users browsing this forum: Google [Bot] and 12 guests