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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
/*
The MIT License (MIT)
Copyright (c) 2017 Lancaster University.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "MicroBitAccelerometer.h"
#include "ErrorNo.h"
#include "MicroBitEvent.h"
#include "MicroBitCompat.h"
#include "MicroBitFiber.h"
#include "MicroBitDevice.h"
#include "MicroBitI2C.h"
#include "MMA8653.h"
#include "FXOS8700.h"
#include "LSM303Accelerometer.h"
/**
* Constructor.
* Create a software abstraction of an FXSO8700 combined accelerometer/magnetometer
*
* @param _i2c an instance of I2C used to communicate with the device.
*
* @param address the default I2C address of the accelerometer. Defaults to: FXS8700_DEFAULT_ADDR.
*
*/
MicroBitAccelerometer::MicroBitAccelerometer(CoordinateSpace &cspace, uint16_t id) : sample(), sampleENU(), coordinateSpace(cspace)
{
// Store our identifiers.
this->id = id;
this->status = 0;
// Set a default rate of 50Hz and a +/-2g range.
this->samplePeriod = 20;
this->sampleRange = 2;
// Initialise gesture history
this->sigma = 0;
this->impulseSigma = 0;
this->lastGesture = MICROBIT_ACCELEROMETER_EVT_NONE;
this->currentGesture = MICROBIT_ACCELEROMETER_EVT_NONE;
this->shake.x = 0;
this->shake.y = 0;
this->shake.z = 0;
this->shake.count = 0;
this->shake.timer = 0;
this->shake.impulse_3 = 1;
this->shake.impulse_6 = 1;
this->shake.impulse_8 = 1;
}
/**
* Device autodetection. Scans the given I2C bus for supported accelerometer devices.
* if found, constructs an appropriate driver and returns it.
*
* @param i2c the bus to scan.
* @param id the unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER
*
*/
MicroBitAccelerometer& MicroBitAccelerometer::autoDetect(MicroBitI2C &i2c)
{
if (MicroBitAccelerometer::detectedAccelerometer == NULL)
{
// Configuration of IRQ lines
MicroBitPin int1(MICROBIT_ID_IO_INT1, P0_28, PIN_CAPABILITY_STANDARD);
MicroBitPin int2(MICROBIT_ID_IO_INT2, P0_29, PIN_CAPABILITY_STANDARD);
MicroBitPin int3(MICROBIT_ID_IO_INT3, P0_27, PIN_CAPABILITY_STANDARD);
// All known accelerometer/magnetometer peripherals have the same alignment
CoordinateSpace &coordinateSpace = *(new CoordinateSpace(SIMPLE_CARTESIAN, true, COORDINATE_SPACE_ROTATED_0));
// Now, probe for connected peripherals, if none have already been found.
if (MMA8653::isDetected(i2c))
MicroBitAccelerometer::detectedAccelerometer = new MMA8653(i2c, int1, coordinateSpace);
else if (LSM303Accelerometer::isDetected(i2c))
MicroBitAccelerometer::detectedAccelerometer = new LSM303Accelerometer(i2c, int1, coordinateSpace);
else if (FXOS8700::isDetected(i2c))
{
FXOS8700 *fxos = new FXOS8700(i2c, int3, coordinateSpace);
MicroBitAccelerometer::detectedAccelerometer = fxos;
MicroBitCompass::detectedCompass = fxos;
}
// Insert this case to support FXOS on the microbit1.5-SN
//else if (FXOS8700::isDetected(i2c, 0x3A))
//{
// FXOS8700 *fxos = new FXOS8700(i2c, int3, coordinateSpace, 0x3A);
// MicroBitAccelerometer::detectedAccelerometer = fxos;
// MicroBitCompass::detectedCompass = fxos;
//}
else
{
MicroBitAccelerometer *unavailable = new MicroBitAccelerometer(coordinateSpace, MICROBIT_ID_ACCELEROMETER);
MicroBitAccelerometer::detectedAccelerometer = unavailable;
}
}
if (MicroBitCompass::detectedCompass)
MicroBitCompass::detectedCompass->setAccelerometer(*MicroBitAccelerometer::detectedAccelerometer);
return *MicroBitAccelerometer::detectedAccelerometer;
}
/**
* Stores data from the accelerometer sensor in our buffer, and perform gesture tracking.
*
* On first use, this member function will attempt to add this component to the
* list of fiber components in order to constantly update the values stored
* by this object.
*
* This lazy instantiation means that we do not
* obtain the overhead from non-chalantly adding this component to fiber components.
*
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the read request fails.
*/
int MicroBitAccelerometer::update()
{
// Store the new data, after performing any necessary coordinate transformations.
sample = coordinateSpace.transform(sampleENU);
// Indicate that pitch and roll data is now stale, and needs to be recalculated if needed.
status &= ~MICROBIT_ACCELEROMETER_IMU_DATA_VALID;
// Update gesture tracking
updateGesture();
// Indicate that a new sample is available
MicroBitEvent e(id, MICROBIT_ACCELEROMETER_EVT_DATA_UPDATE);
return MICROBIT_OK;
};
/**
* A service function.
* It calculates the current scalar acceleration of the device (x^2 + y^2 + z^2).
* It does not, however, square root the result, as this is a relatively high cost operation.
*
* This is left to application code should it be needed.
*
* @return the sum of the square of the acceleration of the device across all axes.
*/
uint32_t MicroBitAccelerometer::instantaneousAccelerationSquared()
{
// Use pythagoras theorem to determine the combined force acting on the device.
return (uint32_t)sample.x*(uint32_t)sample.x + (uint32_t)sample.y*(uint32_t)sample.y + (uint32_t)sample.z*(uint32_t)sample.z;
}
/**
* Service function.
* Determines a 'best guess' posture of the device based on instantaneous data.
*
* This makes no use of historic data, and forms the input to the filter implemented in updateGesture().
*
* @return A 'best guess' of the current posture of the device, based on instanataneous data.
*/
uint16_t MicroBitAccelerometer::instantaneousPosture()
{
bool shakeDetected = false;
// Test for shake events.
// We detect a shake by measuring zero crossings in each axis. In other words, if we see a strong acceleration to the left followed by
// a strong acceleration to the right, then we can infer a shake. Similarly, we can do this for each axis (left/right, up/down, in/out).
//
// If we see enough zero crossings in succession (MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD), then we decide that the device
// has been shaken.
if ((sample.x < -MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && shake.x) || (sample.x > MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && !shake.x))
{
shakeDetected = true;
shake.x = !shake.x;
}
if ((sample.y < -MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && shake.y) || (sample.y > MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && !shake.y))
{
shakeDetected = true;
shake.y = !shake.y;
}
if ((sample.z < -MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && shake.z) || (sample.z > MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE && !shake.z))
{
shakeDetected = true;
shake.z = !shake.z;
}
// If we detected a zero crossing in this sample period, count this.
if (shakeDetected && shake.count < MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD)
{
shake.count++;
if (shake.count == 1)
shake.timer = 0;
if (shake.count == MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD)
{
shake.shaken = 1;
shake.timer = 0;
return MICROBIT_ACCELEROMETER_EVT_SHAKE;
}
}
// measure how long we have been detecting a SHAKE event.
if (shake.count > 0)
{
shake.timer++;
// If we've issued a SHAKE event already, and sufficient time has assed, allow another SHAKE event to be issued.
if (shake.shaken && shake.timer >= MICROBIT_ACCELEROMETER_SHAKE_RTX)
{
shake.shaken = 0;
shake.timer = 0;
shake.count = 0;
}
// Decay our count of zero crossings over time. We don't want them to accumulate if the user performs slow moving motions.
else if (!shake.shaken && shake.timer >= MICROBIT_ACCELEROMETER_SHAKE_DAMPING)
{
shake.timer = 0;
if (shake.count > 0)
shake.count--;
}
}
uint32_t force = instantaneousAccelerationSquared();
if (force < MICROBIT_ACCELEROMETER_FREEFALL_THRESHOLD)
return MICROBIT_ACCELEROMETER_EVT_FREEFALL;
// Determine our posture.
if (sample.x < (-1000 + MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return MICROBIT_ACCELEROMETER_EVT_TILT_LEFT;
if (sample.x > (1000 - MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return MICROBIT_ACCELEROMETER_EVT_TILT_RIGHT;
if (sample.y < (-1000 + MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return MICROBIT_ACCELEROMETER_EVT_TILT_DOWN;
if (sample.y > (1000 - MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return MICROBIT_ACCELEROMETER_EVT_TILT_UP;
if (sample.z < (-1000 + MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return MICROBIT_ACCELEROMETER_EVT_FACE_UP;
if (sample.z > (1000 - MICROBIT_ACCELEROMETER_TILT_TOLERANCE))
return MICROBIT_ACCELEROMETER_EVT_FACE_DOWN;
return MICROBIT_ACCELEROMETER_EVT_NONE;
}
/**
* Updates the basic gesture recognizer. This performs instantaneous pose recognition, and also some low pass filtering to promote
* stability.
*/
void MicroBitAccelerometer::updateGesture()
{
// Check for High/Low G force events - typically impulses, impacts etc.
// Again, during such spikes, these event take priority of the posture of the device.
// For these events, we don't perform any low pass filtering.
uint32_t force = instantaneousAccelerationSquared();
if (force > MICROBIT_ACCELEROMETER_3G_THRESHOLD)
{
if (force > MICROBIT_ACCELEROMETER_3G_THRESHOLD && !shake.impulse_3)
{
MicroBitEvent e(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_3G);
shake.impulse_3 = 1;
}
if (force > MICROBIT_ACCELEROMETER_6G_THRESHOLD && !shake.impulse_6)
{
MicroBitEvent e(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_6G);
shake.impulse_6 = 1;
}
if (force > MICROBIT_ACCELEROMETER_8G_THRESHOLD && !shake.impulse_8)
{
MicroBitEvent e(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_8G);
shake.impulse_8 = 1;
}
impulseSigma = 0;
}
// Reset the impulse event onve the acceleration has subsided.
if (impulseSigma < MICROBIT_ACCELEROMETER_GESTURE_DAMPING)
impulseSigma++;
else
shake.impulse_3 = shake.impulse_6 = shake.impulse_8 = 0;
// Determine what it looks like we're doing based on the latest sample...
uint16_t g = instantaneousPosture();
if (g == MICROBIT_ACCELEROMETER_EVT_SHAKE)
{
lastGesture = MICROBIT_ACCELEROMETER_EVT_SHAKE;
MicroBitEvent e(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE);
return;
}
// Perform some low pass filtering to reduce jitter from any detected effects
if (g == currentGesture)
{
if (sigma < MICROBIT_ACCELEROMETER_GESTURE_DAMPING)
sigma++;
}
else
{
currentGesture = g;
sigma = 0;
}
// If we've reached threshold, update our record and raise the relevant event...
if (currentGesture != lastGesture && sigma >= MICROBIT_ACCELEROMETER_GESTURE_DAMPING)
{
lastGesture = currentGesture;
MicroBitEvent e(MICROBIT_ID_GESTURE, lastGesture);
}
}
/**
* Attempts to set the sample rate of the accelerometer to the specified value (in ms).
*
* @param period the requested time between samples, in milliseconds.
*
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
*
* @code
* // sample rate is now 20 ms.
* accelerometer.setPeriod(20);
* @endcode
*
* @note The requested rate may not be possible on the hardware. In this case, the
* nearest lower rate is chosen.
*/
int MicroBitAccelerometer::setPeriod(int period)
{
int result;
samplePeriod = period;
result = configure();
samplePeriod = getPeriod();
return result;
}
/**
* Reads the currently configured sample rate of the accelerometer.
*
* @return The time between samples, in milliseconds.
*/
int MicroBitAccelerometer::getPeriod()
{
return (int)samplePeriod;
}
/**
* Attempts to set the sample range of the accelerometer to the specified value (in g).
*
* @param range The requested sample range of samples, in g.
*
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
*
* @code
* // the sample range of the accelerometer is now 8G.
* accelerometer.setRange(8);
* @endcode
*
* @note The requested range may not be possible on the hardware. In this case, the
* nearest lower range is chosen.
*/
int MicroBitAccelerometer::setRange(int range)
{
int result;
sampleRange = range;
result = configure();
sampleRange = getRange();
return result;
}
/**
* Reads the currently configured sample range of the accelerometer.
*
* @return The sample range, in g.
*/
int MicroBitAccelerometer::getRange()
{
return (int)sampleRange;
}
/**
* Configures the accelerometer for G range and sample rate defined
* in this object. The nearest values are chosen to those defined
* that are supported by the hardware. The instance variables are then
* updated to reflect reality.
*
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the accelerometer could not be configured.
*
* @note This method should be overidden by the hardware driver to implement the requested
* changes in hardware.
*/
int MicroBitAccelerometer::configure()
{
return MICROBIT_NOT_SUPPORTED;
}
/**
* Poll to see if new data is available from the hardware. If so, update it.
* n.b. it is not necessary to explicitly call this funciton to update data
* (it normally happens in the background when the scheduler is idle), but a check is performed
* if the user explicitly requests up to date data.
*
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the update fails.
*
* @note This method should be overidden by the hardware driver to implement the requested
* changes in hardware.
*/
int MicroBitAccelerometer::requestUpdate()
{
microbit_panic(MICROBIT_HARDWARE_UNAVAILABLE_ACC);
return MICROBIT_NOT_SUPPORTED;
}
/**
* Reads the last accelerometer value stored, and provides it in the coordinate system requested.
*
* @param coordinateSpace The coordinate system to use.
* @return The force measured in each axis, in milli-g.
*/
Sample3D MicroBitAccelerometer::getSample(CoordinateSystem coordinateSystem)
{
requestUpdate();
return coordinateSpace.transform(sampleENU, coordinateSystem);
}
/**
* Reads the last accelerometer value stored, and in the coordinate system defined in the constructor.
* @return The force measured in each axis, in milli-g.
*/
Sample3D MicroBitAccelerometer::getSample()
{
requestUpdate();
return sample;
}
/**
* reads the value of the x axis from the latest update retrieved from the accelerometer,
* usingthe default coordinate system as specified in the constructor.
*
* @return the force measured in the x axis, in milli-g.
*/
int MicroBitAccelerometer::getX()
{
requestUpdate();
return sample.x;
}
/**
* reads the value of the y axis from the latest update retrieved from the accelerometer,
* usingthe default coordinate system as specified in the constructor.
*
* @return the force measured in the y axis, in milli-g.
*/
int MicroBitAccelerometer::getY()
{
requestUpdate();
return sample.y;
}
/**
* reads the value of the z axis from the latest update retrieved from the accelerometer,
* usingthe default coordinate system as specified in the constructor.
*
* @return the force measured in the z axis, in milli-g.
*/
int MicroBitAccelerometer::getZ()
{
requestUpdate();
return sample.z;
}
/**
* Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.
*
* @return The pitch of the device, in degrees.
*
* @code
* accelerometer.getPitch();
* @endcode
*/
int MicroBitAccelerometer::getPitch()
{
return (int) ((360*getPitchRadians()) / (2*PI));
}
/**
* Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.
*
* @return The pitch of the device, in radians.
*
* @code
* accelerometer.getPitchRadians();
* @endcode
*/
float MicroBitAccelerometer::getPitchRadians()
{
requestUpdate();
if (!(status & MICROBIT_ACCELEROMETER_IMU_DATA_VALID))
recalculatePitchRoll();
return pitch;
}
/**
* Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.
*
* @return The roll of the device, in degrees.
*
* @code
* accelerometer.getRoll();
* @endcode
*/
int MicroBitAccelerometer::getRoll()
{
return (int) ((360*getRollRadians()) / (2*PI));
}
/**
* Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.
*
* @return The roll of the device, in radians.
*
* @code
* accelerometer.getRollRadians();
* @endcode
*/
float MicroBitAccelerometer::getRollRadians()
{
requestUpdate();
if (!(status & MICROBIT_ACCELEROMETER_IMU_DATA_VALID))
recalculatePitchRoll();
return roll;
}
/**
* Recalculate roll and pitch values for the current sample.
*
* @note We only do this at most once per sample, as the necessary trigonemteric functions are rather
* heavyweight for a CPU without a floating point unit.
*/
void MicroBitAccelerometer::recalculatePitchRoll()
{
double x = (double) sample.x;
double y = (double) sample.y;
double z = (double) sample.z;
roll = atan2(x, -z);
pitch = atan2(y, (x*sin(roll) - z*cos(roll)));
#if CONFIG_ENABLED(MICROBIT_FULL_RANGE_PITCH_CALCULATION)
// Handle to the two "negative quadrants", such that we get an output in the +/- 18- degree range.
// This ensures that the pitch values are consistent with the roll values.
if (z > 0.0)
{
double reference = pitch > 0.0 ? (PI / 2.0) : (-PI / 2.0);
pitch = reference + (reference - pitch);
}
#endif
status |= MICROBIT_ACCELEROMETER_IMU_DATA_VALID;
}
/**
* Retrieves the last recorded gesture.
*
* @return The last gesture that was detected.
*
* Example:
* @code
*
* if (accelerometer.getGesture() == SHAKE)
* display.scroll("SHAKE!");
* @endcode
*/
uint16_t MicroBitAccelerometer::getGesture()
{
return lastGesture;
}
/**
* Destructor for FXS8700, where we deregister from the array of fiber components.
*/
MicroBitAccelerometer::~MicroBitAccelerometer()
{
}
MicroBitAccelerometer* MicroBitAccelerometer::detectedAccelerometer = NULL;
|