2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: unit_test_001.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// DATE: 2020-12-24
|
|
|
|
// PURPOSE: unit tests for the GY521
|
|
|
|
// https://github.com/RobTillaart/GY521
|
|
|
|
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
|
|
|
|
//
|
|
|
|
|
|
|
|
// supported assertions
|
|
|
|
// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42
|
|
|
|
// ----------------------------
|
|
|
|
// assertEqual(expected, actual)
|
|
|
|
// assertNotEqual(expected, actual)
|
|
|
|
// assertLess(expected, actual)
|
|
|
|
// assertMore(expected, actual)
|
|
|
|
// assertLessOrEqual(expected, actual)
|
|
|
|
// assertMoreOrEqual(expected, actual)
|
|
|
|
// assertTrue(actual)
|
|
|
|
// assertFalse(actual)
|
|
|
|
// assertNull(actual)
|
|
|
|
// assertNotNull(actual)
|
|
|
|
|
2021-10-20 12:48:35 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#include <ArduinoUnitTests.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "GY521.h"
|
|
|
|
|
|
|
|
|
|
|
|
unittest_setup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:48:35 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
unittest_teardown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_constructor)
|
|
|
|
{
|
|
|
|
GY521 sensor(0x69);
|
2021-10-20 12:48:35 -04:00
|
|
|
fprintf(stderr, "VERSION: %s\n", (char *) GY521_LIB_VERSION);
|
2021-01-29 06:31:58 -05:00
|
|
|
sensor.begin();
|
|
|
|
assertEqual(GY521_OK, sensor.getError());
|
|
|
|
|
2021-06-13 10:00:27 -04:00
|
|
|
assertTrue(sensor.isConnected()); // incorrect but OK
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-13 10:00:27 -04:00
|
|
|
unittest(test_get_set_throttle)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
GY521 sensor(0x69);
|
|
|
|
sensor.begin();
|
|
|
|
assertEqual(GY521_OK, sensor.getError());
|
2021-06-13 10:00:27 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
sensor.setThrottle(true);
|
|
|
|
assertTrue(sensor.getThrottle());
|
|
|
|
sensor.setThrottle(false);
|
|
|
|
assertFalse(sensor.getThrottle());
|
2021-06-13 10:00:27 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
fprintf(stderr, "setThrottleTime()\n");
|
|
|
|
for (uint16_t ti = 1; ti != 0; ti <<= 1)
|
|
|
|
{
|
|
|
|
sensor.setThrottleTime(ti);
|
|
|
|
fprintf(stderr, "%d\t", sensor.getThrottleTime());
|
|
|
|
assertEqual(ti, sensor.getThrottleTime());
|
|
|
|
}
|
2021-06-13 10:00:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_get_set_sensitivity)
|
|
|
|
{
|
|
|
|
GY521 sensor(0x69);
|
|
|
|
sensor.begin();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
fprintf(stderr, "setAccelSensitivity() - fails \n");
|
|
|
|
for (int as = 0; as < 4; as++)
|
|
|
|
{
|
|
|
|
sensor.setAccelSensitivity(as);
|
|
|
|
// fprintf(stderr, "%d\n", sensor.getAccelSensitivity());
|
|
|
|
assertEqual(255, sensor.getAccelSensitivity());
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "setGyroSensitivity() - fails \n");
|
|
|
|
for (int gs = 0; gs < 4; gs++)
|
|
|
|
{
|
|
|
|
sensor.setGyroSensitivity(gs);
|
|
|
|
// fprintf(stderr, "%d\n", sensor.getAccelSensitivity());
|
|
|
|
assertEqual(255, sensor.getAccelSensitivity());
|
|
|
|
}
|
2021-06-13 10:00:27 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-06-13 10:00:27 -04:00
|
|
|
unittest(test_constants)
|
|
|
|
{
|
2021-10-20 12:48:35 -04:00
|
|
|
fprintf(stderr, "VERSION: %s\n", (char *) GY521_LIB_VERSION);
|
2021-06-13 10:00:27 -04:00
|
|
|
|
|
|
|
assertEqual(GY521_OK, 0);
|
|
|
|
assertEqual(GY521_THROTTLED, 1);
|
|
|
|
assertEqual(GY521_ERROR_READ, -1);
|
|
|
|
assertEqual(GY521_ERROR_WRITE, -2);
|
|
|
|
assertEqual(GY521_ERROR_NOT_CONNECTED, -3);
|
|
|
|
|
|
|
|
assertEqual(GY521_THROTTLE_TIME, 10);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-06-13 10:00:27 -04:00
|
|
|
|
|
|
|
unittest(test_initial_values)
|
|
|
|
{
|
|
|
|
GY521 sensor(0x69);
|
|
|
|
|
|
|
|
assertEqualFloat(0, sensor.getAccelX(), 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.getAccelY(), 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.getAccelZ(), 0.0001);
|
|
|
|
|
|
|
|
assertEqualFloat(0, sensor.getAngleX(), 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.getAngleY(), 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.getAngleZ(), 0.0001);
|
|
|
|
|
|
|
|
assertEqualFloat(0, sensor.getGyroX(), 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.getGyroY(), 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.getGyroZ(), 0.0001);
|
|
|
|
|
|
|
|
assertEqualFloat(0, sensor.getPitch(), 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.getRoll(), 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.getYaw(), 0.0001);
|
|
|
|
|
|
|
|
fprintf(stderr, "\nother values()\n");
|
|
|
|
assertEqualFloat(0, sensor.getTemperature(), 0.0001);
|
|
|
|
assertEqual(0, sensor.lastTime() );
|
|
|
|
assertEqual(0, sensor.getError() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unittest(test_initial_calibration_errors)
|
|
|
|
{
|
|
|
|
GY521 sensor(0x69);
|
|
|
|
|
|
|
|
assertEqualFloat(0, sensor.axe, 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.aye, 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.aze, 0.0001);
|
|
|
|
|
|
|
|
assertEqualFloat(0, sensor.gxe, 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.gye, 0.0001);
|
|
|
|
assertEqualFloat(0, sensor.gze, 0.0001);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
unittest_main()
|
|
|
|
|
|
|
|
// --------
|