Presentation is loading. Please wait.

Presentation is loading. Please wait.

Calculating acceleration using ADXL213 – dual axis accelerometer.

Similar presentations


Presentation on theme: "Calculating acceleration using ADXL213 – dual axis accelerometer."— Presentation transcript:

1 Calculating acceleration using ADXL213 – dual axis accelerometer

2 2 / 28 Cargo Monitoring System Disaster likely to be in remote areas Cargo to be delivered by skid-air drop During transport  Quick indication of health of product NOW  Acceleration in range – accuracy of +- 1/8 G  Temperature steady – accuracy of +- 1 / 32 C On delivery  Display of ranges that cargo has experienced  Range of temperatures and accelerations  Other cargo information

3 3 / 28 Overall design Initialize stuff (C++) Calculate Temperature Store temperature, calculate averages and ranges Calculate Acceleration Store acceleration, calculate averages and ranges General Purpose Timer controlling Display as ISR Temperature / Acceleration graphic (non-text) display Changes, actual temperatures Core temperature ISR clock used for Temperature / Acceleration determination Communications with LCD MOSI / MISO format -- ISR Temperature / Acceleration information Text format main( ) Lecture Lab. 3

4 4 / 28 Design details added Initialize stuff (C++) Calculate Temperature Store temperature, calculate averages and ranges Calculate Acceleration Store acceleration, calculate averages and ranges General Purpose Timer controlling Display as ISR Temperature / Acceleration graphic (non-text) display Changes, actual temperatures #define ACCELERATION_STEADY 1 #define ACCELERATION_DECREASING 2 #define ACCELERATION_INCREASING 3 variable acceleration_changing Communication between main( ) and ISR main( )

5 5 / 28 Three files are going to be needed Lab3 directory – where product will end up being built lab3prototypes.h #define ACCELERATION_STEADY 1 #define ACCELERATION_DECREASING 2 #define ACCELERATION_INCREASING 3 void Set_Acceleration_Mode(long int current_Acc, long int average_ACC); CodeAcceleration.cpp Set_Acceleration_Mode( current_Acc, average_ACC) { All necessary code to make function work } Lab3 tests directory – where all tests will be built TestsAcceleration.cpp TEST(Set_Acceleration_Mode, DEVELOPER_TEST) { All necessary code to test that function works }

6 6 / 28 Set_Acceleration_Mode( ) Test Recap #include “../Lab3/lab3prototypes.h” extern volatile long int acceleration_changing; // Declared in CodeAcceleration.cpp // Lab3 directory -- global TEST(Set_Acceleration_Mode, DEVELOPER_TEST) // acceleration_now == average_acceleration Set_Acceleration_Mode(6, 6); CHECK(acceleration_changing == ACCELERATION_STEADY); // acceleration_now < average_acceleration Set_Acceleration_Mode(0, 6); CHECK(acceleration_changing == ACCELERATION_DECREASING); // acceleration_now > average_acceleration Set_Acceleration_Mode(6, 0) CHECK(acceleration_changing == ACCELERATION_INCREASING); } Then we wrote the code to satisfy the test

7 7 / 28 Recap -- test – Ability to calculate Average #include “../Lab3/lab3prototypes.h” TEST(AverageCalculation, DEVELOPER_TEST) int previous_values[10] = {0, 0, 2, 2, 1, 1, 10, 10, 10, 10}; int average_value = 0; bool result = true; // Empty array -- invalid number of points as array length result = CalculateAverage(previous_values, 0, &average_value); CHECK(result == false); // Average first two values average_value = 6; result = CalculateAverage(previous_values, 2, &average_value); CHECK(result == true); CHECK(average_value == 0); // Average first four values result = CalculateAverage(previous_values, 4, &average_value); CHECK(result == true); CHECK(average_value == 1); etc. Then we wrote the code to satisfy the test

8 8 / 28 Recap -- Tests for bool AddToArray(int *, int, int) #include “../Lab3/lab3prototypes.h” TEST(AddToArray, DEVELOPER_TEST) { #define MAX_ARRAY_SIZE 8 // WOULD THIS BE BETTER DEFINED INSIDE../Lab3/lab3prototypes.h? int previous_values[MAX_ARRAY_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0}; bool result; // Have a new acceleration value of 1 – add to the array int expected [MAX_ARRAY_SIZE] = {1, 0, 0, 0, 0, 0, 0, 0}; result = AddToArray(previous_values, MAX_ARRAY_SIZE, 1); CHECK(result == true); ARRAYS_EQUAL(expected1, previous_values, MAX_ARRAY_SIZE); // Have new acceleration values of 2 and then 3 – add those to the array int expected2[MAX_ARRAY_SIZE] = {1, 2, 3, 0, 0, 0, 0, 0}; result = AddToArray(previous_values, MAX_ARRAY_SIZE, 2); CHECK(result == true); result = AddToArray(previous_values, MAX_ARRAY_SIZE, 3); CHECK(result == true); ARRAYS_EQUAL(expected2, previous_values, MAX_ARRAY_SIZE); …………………………MORE TEST CODE ON NEXT PAGE ……………………………. Then we wrote the code to satisfy the test

9 9 / 28 Calculation the acceleration using information from the hardware Let us assume that we have the time (in clock pulses) for T1 (T1_high) and T2 (T2_period) Need to develop the tests to check that correctly calculate the acceleration when the acceleration is in the range +1.7G to -1.7G bool CalculateAcceleration(int T1_high, int T2_period, int *new_acceleration_value)

10 10 / 28 You were asked Before tomorrow’s class Write the tests needed to show that bool CalculateAcceleration(int T1_high, int T2_period, int *new_acceleration_value) correctly calculates the acceleration when the acceleration is in the range +1.7G to -1.7G Through this test design – identify the “design defect” in the current project design concept for the transportation monitoring device

11 11 / 28 Select a volun-tell to write this test T1 is half of T2 – 50% duty cycle  Manual says result should be 0 g CalculateAcceleration(1, 2, new_acceleration_value == 0 g) TEST(HALF_DUTY_CYCLE, DEVELOPER_TEST) { }

12 12 / 28 Select a volun-tell to write this test T1 = 0  Manual says A(g) = (0 – 0.5) / 30% = -0.5 / 30%  What does -0.5 / 30% mean?  Engineering guess – poor description. Do they mean 30% is to be read as 0.30? T1 = 0 therefore means A(g) = (0 – 0.5 ) / 0.3 = -1.666g. Would make some sense – since data book says accelerometer works 1.7G to -1.7G TEST(ZERO_DUTY_CYCLE, DEVELOPER_TEST) { }

13 13 / 28 You write the test for correct result T1 = T2 TEST(HUNDREDPERCENT_DUTY_CYCLE, DEVELOPER_TEST) { }

14 14 / 28 You write the test for correct result Should return false when T2 = 0 or negative Should return false if T1 > T2 or T1 < 0 TEST(BAD_T1_OR_T2, DEVELOPER_TEST) { }

15 15 / 28 ADXL213 “tilt sensor” mode info X 50% Y 50% X 50%  0g Y 80%  1g X 50%  0g Y 20%  -1g

16 16 / 28 You write the test for tilt mode TEST(TILT TEST, DEVELOPER_TEST) { // Should there be 5 asserts to match the known values in Fig. 21? }

17 17 / 28 Problem Some people have all tests pass – depends on the tests written  This group would move on, assuming everything is correct  However this group has a DESIGN DEFECT which will cause problems later on as the DEFECT will be very difficult to identify (w.r.t. location) Some people have most tests fail  Some of this group will change their CODE until everything is correct. They spotted a DESIGN ERROR and fixed it before it caused a problem  Some of this group will change their TESTS until everything is correct. However this group NOW has a DESIGN DEFECT which will cause problems later on as the DEFECT will be very difficult to identify (w.r.t. location) Correct answer – most tests should be failing Why the difference? Depends on how you wrote the test

18 18 / 28 Calculate_Acceleration_Mode( )

19 19 / 28 Understanding why the errors We need to understand what sort of assembly code will be generated by the compiler int new_acceleration = 1000; // Use ridiculous starting values int expected_value = 1.6666; CalculateAcceleration(1, 1, &new_acceleration); CHECK(new_acceleration == expected_value); CHECK(new_acceleration == 1.666); // ASSERT FAILS

20 20 / 28 Understanding why the errors // Stack picture // OLD RTS  FP + 4 -- needed for P0 = [FP + 4]; JUMP (P0); // OLD FP  FP // storage for new_acceleration  FP – 4 or SP + 20 // storage for expected _value  FP – 8 or SP + 16 LINK (16 + 2 *4); // Need to place two variables on the stack // int new_acceleration = 1000; // Use ridiculous starting values R3 = 1000 (X); [FP – 4] = R3; // address of new_acceleration used in subroutine call // Must have value on stack as need AN ADDRESS // int expected_value = 1.6666; R2 = 1 (X); [FP – 8] = R2; // R2 will be destroyed during subroutine call // Place value on stack as need after subroutine call // CalculateAcceleration(1, 1, &new_acceleration); R2 = FP; R2 += - 4; // Address of new_acceleration on stack – last Friday’s lecture R1 = 1; R0 = R1; CALL _CalculateAcceleration__FiT1Pi

21 21 / 28 Understanding why the errors // int expected_value = 1.6666; R2 = 1 (X); [FP – 8] = R4; This is an integer processor -- floating point number 1.6666 is rounded down to 1 (might be rounded up to 2) by the compiler preprocessor SOLUTION 1 -- Use floats rather than integers Use float expected_value = 1.6666; Do all calculations using floating point operations rather than integer Most integer processor compilers WILL support floating point operations But this processor is an integer processor, so the compiler will use “software implementation” of floating point operations – roughly 200 to 300 times slower than equivalent integer operations. (Why?) Remember – will have to modify all operations on temperatures to support floating point operations – Store-to-array, Calculate-Average, SetMax etc

22 22 / 28 Understanding why the errors // int expected_value = 1.6666; R2 = 1 (X); [FP – 8] = R4; This is an integer processor -- floating point number 1.6666 is rounded down to 1 (might be rounded up to 2) by the compiler preprocessor SOLUTION 2 – Scale values by 0x10000 0g represented by 0x00000 1 g represented by 0x10000 0.5g represented by 0x08000 (which is 0x10000 >> 1) 0.25g represented by 0x04000 (which is 0x10000 >> 2) Will this work for the processor? Addition works when doing this – ditto subtraction 1.5g represented by 0x18000 (0x10000 + 0x08000) Multiplication works 0.5g * 2 = 0x8000 * 2 = 0x10000 = 1 g Division works – if we are careful 1.666666 = 5 / 3 * 0x10000 = 1 * 0x10000 = 0x10000 = 1 – wrong answer 1.666666 = (0x10000 * 5) / 3 = 0x50000 / 3 = 0x1AAAA = 1.66666 g or CLOSE TO IT NOT ALL POSSIBLE VALUES CAN BE REPRESENTED IN A COMPUTER ROUND-OFF ERROR (TRUNCATION ERROR, QUANTIZATION ERROR)

23 23 / 28 Improved tests and function Almost passes tests – Accuracy problem?

24 24 / 28 REVIEW – How are floating point numbers stored in a computer as a binary values Use VDSP tool to see values displayed as integers Displayed as floats Values 20 20.0 10 10.0 5 5.0 1 1.0

25 25 / 28 REVIEW – How are floating point numbers stored in a computer as a binary values Integer 20 = 16 + 4 = 0x14 = b00010100 Floating point 20.0 = 0x14 = b00010100 as integer = 0x1.4 * 2 4 = b1.1000 * 2 4 normalized Convention – normalized values are in the form b1.frac * 2 exponent

26 26 / 28 REVIEW – How are floating point numbers stored in a computer as a binary values Floating point 20.0 = 0x14 = b00010100 as integer = 0x1.4 * 2 4 = + b1.0100 * 2 4 normalized IEEE Convention – normalized values are in the form + b1.frac * 2 exponent IEEE Convention – normalized values are stored as 1 bit 8-bits 23-bits sign bit biased exponent fractional part exponent + 127 b0 127 + 4 b0100 0000 0000 0000 0000 000 b0 0x7F + 4 b0100 0000 0000 0000 0000 000 b0 0x83 b0100 0000 0000 0000 0000 000 b0 b10000011 b0100 0000 0000 0000 0000 000 b0100 0001 1010 0000 0000 0000 0000 0000 0x4 1 A 0 0 0 0 0

27 27 / 28 You should be able to convert over decimal values to floating point format 2.5 0.25 128.625 Then use the VDSP register display window to display the values and check if you have the correct answer

28 28 / 28 Done today Reviewed the Lab. 3 project Identified a major design flaw Modified the code to fix the design flaw  Almost fixed the problem with just one line changed in code  Have identified a possible accuracy error Can we “completely” fix the accuracy problem with further improvements in code design? Is this a fundamental limitation of the algorithm and the tests need to be modified to incorporate the limitation? Review of knowledge issues related to design flaw  Understand how integer and floating numbers are compared in a computer program  Understand how integer and floating numbers are stored in a computer program


Download ppt "Calculating acceleration using ADXL213 – dual axis accelerometer."

Similar presentations


Ads by Google