Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
TestAccelerometer.cpp@21:a111be2582be, 2017-01-18 (annotated)
- Committer:
- dupm2216
- Date:
- Wed Jan 18 02:38:05 2017 +0000
- Revision:
- 21:a111be2582be
- Parent:
- 8:862e28c7f6f6
Add code header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dupm2216 | 21:a111be2582be | 1 | ///////////////////////////////////////////////////////////// |
dupm2216 | 21:a111be2582be | 2 | // APP 1: Systèmes à microprocesseurs // |
dupm2216 | 21:a111be2582be | 3 | // // |
dupm2216 | 21:a111be2582be | 4 | // Université de Sherbrooke // |
dupm2216 | 21:a111be2582be | 5 | // Génie informatique // |
dupm2216 | 21:a111be2582be | 6 | // Session 5, Hiver 2017 // |
dupm2216 | 21:a111be2582be | 7 | // // |
dupm2216 | 21:a111be2582be | 8 | // Date: 17 janvier 2017 // |
dupm2216 | 21:a111be2582be | 9 | // // |
dupm2216 | 21:a111be2582be | 10 | // Auteurs: Maxime Dupuis, dupm2216 // |
dupm2216 | 21:a111be2582be | 11 | // Bruno Allaire-Lemay, allb2701 // |
dupm2216 | 21:a111be2582be | 12 | ///////////////////////////////////////////////////////////// |
dupm2216 | 21:a111be2582be | 13 | |
dupm2216 | 3:1a9d0f0a50bf | 14 | #include "TestAccelerometer.hpp" |
dupm2216 | 3:1a9d0f0a50bf | 15 | #include "Accelerometer.hpp" |
dupm2216 | 6:3facf0329142 | 16 | #include "Utility.hpp" |
dupm2216 | 3:1a9d0f0a50bf | 17 | #include "mbed.h" |
dupm2216 | 3:1a9d0f0a50bf | 18 | |
dupm2216 | 3:1a9d0f0a50bf | 19 | #include <cassert> |
dupm2216 | 5:f59b51ac4b40 | 20 | #include <cmath> |
dupm2216 | 3:1a9d0f0a50bf | 21 | |
dupm2216 | 3:1a9d0f0a50bf | 22 | namespace accelerometer |
dupm2216 | 3:1a9d0f0a50bf | 23 | { |
dupm2216 | 3:1a9d0f0a50bf | 24 | void run_all_tests() |
dupm2216 | 3:1a9d0f0a50bf | 25 | { |
dupm2216 | 3:1a9d0f0a50bf | 26 | test_raw_axis_data_to_int(); |
dupm2216 | 3:1a9d0f0a50bf | 27 | test_set_standby_and_active(); |
dupm2216 | 6:3facf0329142 | 28 | test_g_force_from_int_axis_data(); |
dupm2216 | 6:3facf0329142 | 29 | test_angle_from_int_axis_data(); |
dupm2216 | 3:1a9d0f0a50bf | 30 | } |
dupm2216 | 3:1a9d0f0a50bf | 31 | |
dupm2216 | 3:1a9d0f0a50bf | 32 | void test_raw_axis_data_to_int() |
dupm2216 | 3:1a9d0f0a50bf | 33 | { |
dupm2216 | 3:1a9d0f0a50bf | 34 | assert(128 == (unsigned char)(0x80)); |
dupm2216 | 3:1a9d0f0a50bf | 35 | assert(-128 == (signed char)(0x80)); |
dupm2216 | 3:1a9d0f0a50bf | 36 | |
dupm2216 | 3:1a9d0f0a50bf | 37 | assert(0 == raw_axis_data_to_int(0x00)); |
dupm2216 | 3:1a9d0f0a50bf | 38 | assert(1 == raw_axis_data_to_int(0x01)); |
dupm2216 | 3:1a9d0f0a50bf | 39 | assert(127 == raw_axis_data_to_int(0x7F)); |
dupm2216 | 3:1a9d0f0a50bf | 40 | assert(-1 == raw_axis_data_to_int(0xFF)); |
dupm2216 | 3:1a9d0f0a50bf | 41 | assert(-128 == raw_axis_data_to_int(0x80)); |
dupm2216 | 3:1a9d0f0a50bf | 42 | } |
dupm2216 | 3:1a9d0f0a50bf | 43 | |
dupm2216 | 3:1a9d0f0a50bf | 44 | void test_set_standby_and_active() |
dupm2216 | 3:1a9d0f0a50bf | 45 | { |
dupm2216 | 3:1a9d0f0a50bf | 46 | Accelerometer accelerometer(p9, p10, I2C_ACCELEROMETER_ADDRESS); |
dupm2216 | 3:1a9d0f0a50bf | 47 | |
dupm2216 | 3:1a9d0f0a50bf | 48 | accelerometer.set_standby(); |
dupm2216 | 3:1a9d0f0a50bf | 49 | char value = accelerometer.read_register(CTRL_REG1_REGISTER_ADDRESS); |
dupm2216 | 3:1a9d0f0a50bf | 50 | if(value % 2 != 0) |
dupm2216 | 3:1a9d0f0a50bf | 51 | { |
dupm2216 | 3:1a9d0f0a50bf | 52 | printf("Fail\r\n"); |
dupm2216 | 3:1a9d0f0a50bf | 53 | } |
dupm2216 | 3:1a9d0f0a50bf | 54 | |
dupm2216 | 3:1a9d0f0a50bf | 55 | accelerometer.set_active(); |
dupm2216 | 3:1a9d0f0a50bf | 56 | value = accelerometer.read_register(CTRL_REG1_REGISTER_ADDRESS); |
dupm2216 | 3:1a9d0f0a50bf | 57 | if(value % 2 != 1) |
dupm2216 | 3:1a9d0f0a50bf | 58 | { |
dupm2216 | 3:1a9d0f0a50bf | 59 | printf("Fail\r\n"); |
dupm2216 | 3:1a9d0f0a50bf | 60 | } |
dupm2216 | 3:1a9d0f0a50bf | 61 | } |
dupm2216 | 5:f59b51ac4b40 | 62 | |
dupm2216 | 8:862e28c7f6f6 | 63 | //axis_data = 64 means 1 g |
dupm2216 | 5:f59b51ac4b40 | 64 | void test_angle_from_int_axis_data() |
dupm2216 | 5:f59b51ac4b40 | 65 | { |
dupm2216 | 5:f59b51ac4b40 | 66 | const double tolerance = 0.05; |
dupm2216 | 6:3facf0329142 | 67 | assert(utility::is_almost_equal(0, angle_from_int_axis_data(64), tolerance)); |
dupm2216 | 6:3facf0329142 | 68 | assert(utility::is_almost_equal(0, angle_from_int_axis_data(-64), tolerance)); |
dupm2216 | 6:3facf0329142 | 69 | assert(utility::is_almost_equal(60, angle_from_int_axis_data(32), tolerance)); |
dupm2216 | 6:3facf0329142 | 70 | assert(utility::is_almost_equal(60, angle_from_int_axis_data(-32), tolerance)); |
dupm2216 | 5:f59b51ac4b40 | 71 | } |
dupm2216 | 5:f59b51ac4b40 | 72 | |
dupm2216 | 8:862e28c7f6f6 | 73 | void test_angle_from_int_axis_data_when_above_1g_is_corrected_to_zero() |
dupm2216 | 8:862e28c7f6f6 | 74 | { |
dupm2216 | 8:862e28c7f6f6 | 75 | const double tolerance = 0.05; |
dupm2216 | 8:862e28c7f6f6 | 76 | const double axis_data_above_1g = 65; |
dupm2216 | 8:862e28c7f6f6 | 77 | assert(utility::is_almost_equal(0, angle_from_int_axis_data(axis_data_above_1g), tolerance)); |
dupm2216 | 8:862e28c7f6f6 | 78 | } |
dupm2216 | 8:862e28c7f6f6 | 79 | |
dupm2216 | 5:f59b51ac4b40 | 80 | void test_g_force_from_int_axis_data() |
dupm2216 | 5:f59b51ac4b40 | 81 | { |
dupm2216 | 5:f59b51ac4b40 | 82 | const double tolerance = 0.05; |
dupm2216 | 6:3facf0329142 | 83 | assert(utility::is_almost_equal(1, g_force_from_int_axis_data(64), tolerance)); |
dupm2216 | 6:3facf0329142 | 84 | assert(utility::is_almost_equal(-1, g_force_from_int_axis_data(-64), tolerance)); |
dupm2216 | 6:3facf0329142 | 85 | assert(utility::is_almost_equal(2, g_force_from_int_axis_data(128), tolerance)); |
dupm2216 | 6:3facf0329142 | 86 | assert(utility::is_almost_equal(-2, g_force_from_int_axis_data(-128), tolerance)); |
dupm2216 | 6:3facf0329142 | 87 | assert(utility::is_almost_equal(0.5, g_force_from_int_axis_data(32), tolerance)); |
dupm2216 | 6:3facf0329142 | 88 | assert(utility::is_almost_equal(-0.5, g_force_from_int_axis_data(-32), tolerance)); |
dupm2216 | 5:f59b51ac4b40 | 89 | } |
dupm2216 | 3:1a9d0f0a50bf | 90 | } |