|
56 | 56 | NDOF_FMC_OFF_MODE = const(0x0B)
|
57 | 57 | NDOF_MODE = const(0x0C)
|
58 | 58 |
|
59 |
| -ACC_2G = const(0x00) |
60 |
| -ACC_4G = const(0x01) |
61 |
| -ACC_8G = const(0x02) |
62 |
| -ACC_16G = const(0x03) |
63 |
| -ACC_7_81HZ = const(0x00) |
64 |
| -ACC_15_63HZ = const(0x04) |
65 |
| -ACC_31_25HZ = const(0x08) |
66 |
| -ACC_62_5HZ = const(0x0C) |
67 |
| -ACC_125HZ = const(0x10) |
68 |
| -ACC_250HZ = const(0x14) |
69 |
| -ACC_500HZ = const(0x18) |
70 |
| -ACC_1000HZ = const(0x1C) |
71 |
| -ACC_NORMAL_MODE = const(0x00) |
72 |
| -ACC_SUSPEND_MODE = const(0x20) |
73 |
| -ACC_LOWPOWER1_MODE = const(0x40) |
74 |
| -ACC_STANDBY_MODE = const(0x60) |
75 |
| -ACC_LOWPOWER2_MODE = const(0x80) |
76 |
| -ACC_DEEPSUSPEND_MODE = const(0xA0) |
| 59 | +ACCEL_2G = const(0x00) # For accel_range property |
| 60 | +ACCEL_4G = const(0x01) # Default |
| 61 | +ACCEL_8G = const(0x02) |
| 62 | +ACCEL_16G = const(0x03) |
| 63 | +ACCEL_7_81HZ = const(0x00) # For accel_bandwidth property |
| 64 | +ACCEL_15_63HZ = const(0x04) |
| 65 | +ACCEL_31_25HZ = const(0x08) |
| 66 | +ACCEL_62_5HZ = const(0x0C) # Default |
| 67 | +ACCEL_125HZ = const(0x10) |
| 68 | +ACCEL_250HZ = const(0x14) |
| 69 | +ACCEL_500HZ = const(0x18) |
| 70 | +ACCEL_1000HZ = const(0x1C) |
| 71 | +ACCEL_NORMAL_MODE = const(0x00) # Default. For accel_mode property |
| 72 | +ACCEL_SUSPEND_MODE = const(0x20) |
| 73 | +ACCEL_LOWPOWER1_MODE = const(0x40) |
| 74 | +ACCEL_STANDBY_MODE = const(0x60) |
| 75 | +ACCEL_LOWPOWER2_MODE = const(0x80) |
| 76 | +ACCEL_DEEPSUSPEND_MODE = const(0xA0) |
| 77 | + |
| 78 | +GYRO_2000_DPS = const(0x00) # Default. For gyro_range property |
| 79 | +GYRO_1000_DPS = const(0x01) |
| 80 | +GYRO_500_DPS = const(0x02) |
| 81 | +GYRO_250_DPS = const(0x03) |
| 82 | +GYRO_125_DPS = const(0x04) |
| 83 | +GYRO_523HZ = const(0x00) # For gyro_bandwidth property |
| 84 | +GYRO_230HZ = const(0x08) |
| 85 | +GYRO_116HZ = const(0x10) |
| 86 | +GYRO_47HZ = const(0x18) |
| 87 | +GYRO_23HZ = const(0x20) |
| 88 | +GYRO_12HZ = const(0x28) |
| 89 | +GYRO_64HZ = const(0x30) |
| 90 | +GYRO_32HZ = const(0x38) # Default |
| 91 | +GYRO_NORMAL_MODE = const(0x00) # Default. For gyro_mode property |
| 92 | +GYRO_FASTPOWERUP_MODE = const(0x01) |
| 93 | +GYRO_DEEPSUSPEND_MODE = const(0x02) |
| 94 | +GYRO_SUSPEND_MODE = const(0x03) |
| 95 | +GYRO_ADVANCEDPOWERSAVE_MODE = const(0x04) |
| 96 | + |
| 97 | +MAGNET_2HZ = const(0x00) # For magnet_rate property |
| 98 | +MAGNET_6HZ = const(0x01) |
| 99 | +MAGNET_8HZ = const(0x02) |
| 100 | +MAGNET_10HZ = const(0x03) |
| 101 | +MAGNET_15HZ = const(0x04) |
| 102 | +MAGNET_20HZ = const(0x05) # Default |
| 103 | +MAGNET_25HZ = const(0x06) |
| 104 | +MAGNET_30HZ = const(0x07) |
| 105 | +MAGNET_LOWPOWER_MODE = const(0x00) # For magnet_operation_mode property |
| 106 | +MAGNET_REGULAR_MODE = const(0x08) # Default |
| 107 | +MAGNET_ENHANCEDREGULAR_MODE = const(0x10) |
| 108 | +MAGNET_ACCURACY_MODE = const(0x18) |
| 109 | +MAGNET_NORMAL_MODE = const(0x00) # for magnet_power_mode property |
| 110 | +MAGNET_SLEEP_MODE = const(0x20) |
| 111 | +MAGNET_SUSPEND_MODE = const(0x40) |
| 112 | +MAGNET_FORCEMODE_MODE = const(0x60) # Default |
77 | 113 |
|
78 | 114 | _POWER_NORMAL = const(0x00)
|
79 | 115 | _POWER_LOW = const(0x01)
|
80 | 116 | _POWER_SUSPEND = const(0x02)
|
81 | 117 |
|
82 | 118 | _MODE_REGISTER = const(0x3D)
|
83 | 119 | _PAGE_REGISTER = const(0x07)
|
84 |
| -_ACC_CONFIG_REGISTER = const(0x08) |
| 120 | +_ACCEL_CONFIG_REGISTER = const(0x08) |
| 121 | +_MAGNET_CONFIG_REGISTER = const(0x09) |
| 122 | +_GYRO_CONFIG_0_REGISTER = const(0x0A) |
| 123 | +_GYRO_CONFIG_1_REGISTER = const(0x0B) |
85 | 124 | _CALIBRATION_REGISTER = const(0x35)
|
86 | 125 | _OFFSET_ACCEL_REGISTER = const(0x55)
|
87 | 126 | _OFFSET_MAGNET_REGISTER = const(0x5B)
|
@@ -146,6 +185,9 @@ def __init__(self):
|
146 | 185 | self._write_register(_POWER_REGISTER, _POWER_NORMAL)
|
147 | 186 | self._write_register(_PAGE_REGISTER, 0x00)
|
148 | 187 | self._write_register(_TRIGGER_REGISTER, 0x00)
|
| 188 | + self.accel_range = ACCEL_4G |
| 189 | + self.gyro_range = GYRO_2000_DPS |
| 190 | + self.magnet_rate = MAGNET_20HZ |
149 | 191 | time.sleep(0.01)
|
150 | 192 | self.mode = NDOF_MODE
|
151 | 193 | time.sleep(0.01)
|
@@ -347,14 +389,174 @@ def gravity(self):
|
347 | 389 | def _gravity(self):
|
348 | 390 | raise NotImplementedError("Must be implemented.")
|
349 | 391 |
|
350 |
| - def accel_config(self, rng=ACC_4G, bandwidth=ACC_62_5HZ, acc_mode=ACC_NORMAL_MODE): |
351 |
| - """Allows you to set the settings for the accelerometer""" |
352 |
| - if self.mode not in [0x08, 0x09, 0x0A, 0x0B, 0x0C]: |
353 |
| - self._write_register(_ACC_CONFIG_REGISTER, acc_mode | bandwidth | rng) |
354 |
| - time.sleep(0.1) |
355 |
| - value = self._read_register(_ACC_CONFIG_REGISTER) |
356 |
| - return value |
357 |
| - raise RuntimeError("Mode must not be a fusion mode") |
| 392 | + @property |
| 393 | + def accel_range(self): |
| 394 | + """ Switch the accelerometer range and return the new range. Default value: +/- 4g |
| 395 | + See table 3-8 in the datasheet. |
| 396 | + """ |
| 397 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 398 | + value = self._read_register(_ACCEL_CONFIG_REGISTER) |
| 399 | + self._write_register(_PAGE_REGISTER, 0x00) |
| 400 | + return bin(0b00000011 & value) |
| 401 | + |
| 402 | + @accel_range.setter |
| 403 | + def accel_range(self, rng=ACCEL_4G): |
| 404 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 405 | + value = self._read_register(_ACCEL_CONFIG_REGISTER) |
| 406 | + masked_value = 0b11111100 & value |
| 407 | + self._write_register(_ACCEL_CONFIG_REGISTER, masked_value | rng) |
| 408 | + |
| 409 | + @property |
| 410 | + def accel_bandwidth(self): |
| 411 | + """ Switch the accelerometer bandwidth and return the new bandwidth. Default value: 62.5 Hz |
| 412 | + See table 3-8 in the datasheet. |
| 413 | + """ |
| 414 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 415 | + value = self._read_register(_ACCEL_CONFIG_REGISTER) |
| 416 | + self._write_register(_PAGE_REGISTER, 0x00) |
| 417 | + return bin(0b00011100 & value) |
| 418 | + |
| 419 | + @accel_bandwidth.setter |
| 420 | + def accel_bandwidth(self, bandwidth=ACCEL_62_5HZ): |
| 421 | + if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]: |
| 422 | + raise RuntimeError("Mode must not be a fusion mode") |
| 423 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 424 | + value = self._read_register(_ACCEL_CONFIG_REGISTER) |
| 425 | + masked_value = 0b11100011 & value |
| 426 | + self._write_register(_ACCEL_CONFIG_REGISTER, masked_value | bandwidth) |
| 427 | + |
| 428 | + @property |
| 429 | + def accel_mode(self): |
| 430 | + """ Switch the accelerometer mode and return the new mode. Default value: Normal |
| 431 | + See table 3-8 in the datasheet. |
| 432 | + """ |
| 433 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 434 | + value = self._read_register(_ACCEL_CONFIG_REGISTER) |
| 435 | + self._write_register(_PAGE_REGISTER, 0x00) |
| 436 | + return bin(0b11100000 & value) |
| 437 | + |
| 438 | + @accel_mode.setter |
| 439 | + def accel_mode(self, mode=ACCEL_NORMAL_MODE): |
| 440 | + if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]: |
| 441 | + raise RuntimeError("Mode must not be a fusion mode") |
| 442 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 443 | + value = self._read_register(_ACCEL_CONFIG_REGISTER) |
| 444 | + masked_value = 0b00011111 & value |
| 445 | + self._write_register(_ACCEL_CONFIG_REGISTER, masked_value | mode) |
| 446 | + |
| 447 | + @property |
| 448 | + def gyro_range(self): |
| 449 | + """ Switch the gyroscope range and return the new range. Default value: 2000 dps |
| 450 | + See table 3-9 in the datasheet. |
| 451 | + """ |
| 452 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 453 | + value = self._read_register(_GYRO_CONFIG_0_REGISTER) |
| 454 | + self._write_register(_PAGE_REGISTER, 0x00) |
| 455 | + return bin(0b00000111 & value) |
| 456 | + |
| 457 | + @gyro_range.setter |
| 458 | + def gyro_range(self, rng=GYRO_2000_DPS): |
| 459 | + if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]: |
| 460 | + raise RuntimeError("Mode must not be a fusion mode") |
| 461 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 462 | + value = self._read_register(_GYRO_CONFIG_0_REGISTER) |
| 463 | + masked_value = 0b00111000 & value |
| 464 | + self._write_register(_GYRO_CONFIG_0_REGISTER, masked_value | rng) |
| 465 | + |
| 466 | + @property |
| 467 | + def gyro_bandwidth(self): |
| 468 | + """ Switch the gyroscope bandwidth and return the new bandwidth. Default value: 32 Hz |
| 469 | + See table 3-9 in the datasheet. |
| 470 | + """ |
| 471 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 472 | + value = self._read_register(_GYRO_CONFIG_0_REGISTER) |
| 473 | + self._write_register(_PAGE_REGISTER, 0x00) |
| 474 | + return bin(0b00111000 & value) |
| 475 | + |
| 476 | + @gyro_bandwidth.setter |
| 477 | + def gyro_bandwidth(self, bandwidth=GYRO_32HZ): |
| 478 | + if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]: |
| 479 | + raise RuntimeError("Mode must not be a fusion mode") |
| 480 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 481 | + value = self._read_register(_GYRO_CONFIG_0_REGISTER) |
| 482 | + masked_value = 0b00000111 & value |
| 483 | + self._write_register(_GYRO_CONFIG_0_REGISTER, masked_value | bandwidth) |
| 484 | + |
| 485 | + @property |
| 486 | + def gyro_mode(self): |
| 487 | + """ Switch the gyroscope mode and return the new mode. Default value: Normal |
| 488 | + See table 3-9 in the datasheet. |
| 489 | + """ |
| 490 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 491 | + value = self._read_register(_GYRO_CONFIG_1_REGISTER) |
| 492 | + self._write_register(_PAGE_REGISTER, 0x00) |
| 493 | + return bin(0b00000111 & value) |
| 494 | + |
| 495 | + @gyro_mode.setter |
| 496 | + def gyro_mode(self, mode=GYRO_NORMAL_MODE): |
| 497 | + if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]: |
| 498 | + raise RuntimeError("Mode must not be a fusion mode") |
| 499 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 500 | + value = self._read_register(_GYRO_CONFIG_1_REGISTER) |
| 501 | + masked_value = 0b00000000 & value |
| 502 | + self._write_register(_GYRO_CONFIG_1_REGISTER, masked_value | mode) |
| 503 | + |
| 504 | + @property |
| 505 | + def magnet_rate(self): |
| 506 | + """ Switch the magnetometer data output rate and return the new rate. Default value: 20Hz |
| 507 | + See table 3-10 in the datasheet. |
| 508 | + """ |
| 509 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 510 | + value = self._read_register(_MAGNET_CONFIG_REGISTER) |
| 511 | + self._write_register(_PAGE_REGISTER, 0x00) |
| 512 | + return bin(0b00000111 & value) |
| 513 | + |
| 514 | + @magnet_rate.setter |
| 515 | + def magnet_rate(self, rate=MAGNET_20HZ): |
| 516 | + if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]: |
| 517 | + raise RuntimeError("Mode must not be a fusion mode") |
| 518 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 519 | + value = self._read_register(_MAGNET_CONFIG_REGISTER) |
| 520 | + masked_value = 0b01111000 & value |
| 521 | + self._write_register(_MAGNET_CONFIG_REGISTER, masked_value | rate) |
| 522 | + |
| 523 | + @property |
| 524 | + def magnet_operation_mode(self): |
| 525 | + """ Switch the magnetometer operation mode and return the new mode. Default value: Regular |
| 526 | + See table 3-10 in the datasheet. |
| 527 | + """ |
| 528 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 529 | + value = self._read_register(_MAGNET_CONFIG_REGISTER) |
| 530 | + self._write_register(_PAGE_REGISTER, 0x00) |
| 531 | + return bin(0b00011000 & value) |
| 532 | + |
| 533 | + @magnet_operation_mode.setter |
| 534 | + def magnet_operation_mode(self, mode=MAGNET_REGULAR_MODE): |
| 535 | + if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]: |
| 536 | + raise RuntimeError("Mode must not be a fusion mode") |
| 537 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 538 | + value = self._read_register(_MAGNET_CONFIG_REGISTER) |
| 539 | + masked_value = 0b01100111 & value |
| 540 | + self._write_register(_MAGNET_CONFIG_REGISTER, masked_value | mode) |
| 541 | + |
| 542 | + @property |
| 543 | + def magnet_power_mode(self): |
| 544 | + """ Switch the magnetometer power mode and return the new mode. Default value: Forced |
| 545 | + See table 3-10 in the datasheet. |
| 546 | + """ |
| 547 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 548 | + value = self._read_register(_MAGNET_CONFIG_REGISTER) |
| 549 | + self._write_register(_PAGE_REGISTER, 0x00) |
| 550 | + return bin(0b01100000 & value) |
| 551 | + |
| 552 | + @magnet_power_mode.setter |
| 553 | + def magnet_power_mode(self, mode=MAGNET_FORCEMODE_MODE): |
| 554 | + if self.mode in [0x08, 0x09, 0x0A, 0x0B, 0x0C]: |
| 555 | + raise RuntimeError("Mode must not be a fusion mode") |
| 556 | + self._write_register(_PAGE_REGISTER, 0x01) |
| 557 | + value = self._read_register(_MAGNET_CONFIG_REGISTER) |
| 558 | + masked_value = 0b00011111 & value |
| 559 | + self._write_register(_MAGNET_CONFIG_REGISTER, masked_value | mode) |
358 | 560 |
|
359 | 561 | def _write_register(self, register, value):
|
360 | 562 | raise NotImplementedError("Must be implemented.")
|
|
0 commit comments