|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2020-2022 The Pybricks Authors |
| 3 | + |
| 4 | +#include <errno.h> |
| 5 | +#include <signal.h> |
| 6 | +#include <stdint.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | +#include <unistd.h> |
| 11 | +#include <sys/types.h> |
| 12 | +#include <sys/wait.h> |
| 13 | + |
| 14 | +#include <contiki.h> |
| 15 | +#include <tinytest.h> |
| 16 | +#include <tinytest_macros.h> |
| 17 | + |
| 18 | +#include <pbdrv/motor_driver.h> |
| 19 | +#include <pbio/angle.h> |
| 20 | +#include <pbio/control.h> |
| 21 | +#include <pbio/drivebase.h> |
| 22 | +#include <pbio/error.h> |
| 23 | +#include <pbio/logger.h> |
| 24 | +#include <pbio/int_math.h> |
| 25 | +#include <pbio/motor_process.h> |
| 26 | +#include <pbio/servo.h> |
| 27 | +#include <test-pbio.h> |
| 28 | + |
| 29 | +#include "../src/processes.h" |
| 30 | +#include "../drv/core.h" |
| 31 | +#include "../drv/clock/clock_test.h" |
| 32 | +#include "../drv/motor_driver/motor_driver_virtual_simulation.h" |
| 33 | + |
| 34 | +static PT_THREAD(test_drivebase_basics(struct pt *pt)) { |
| 35 | + |
| 36 | + static struct timer timer; |
| 37 | + |
| 38 | + static pbio_servo_t *srv_left; |
| 39 | + static pbio_servo_t *srv_right; |
| 40 | + static pbio_drivebase_t *db; |
| 41 | + |
| 42 | + static int32_t drive_distance; |
| 43 | + static int32_t drive_speed; |
| 44 | + static int32_t drive_acceleration; |
| 45 | + static int32_t drive_deceleration; |
| 46 | + static int32_t turn_angle_start; |
| 47 | + static int32_t turn_angle; |
| 48 | + static int32_t turn_rate; |
| 49 | + static int32_t turn_acceleration; |
| 50 | + static int32_t turn_deceleration; |
| 51 | + |
| 52 | + static bool stalled; |
| 53 | + static uint32_t stall_duration; |
| 54 | + |
| 55 | + // Start motor driver simulation process. |
| 56 | + pbdrv_motor_driver_init_manual(); |
| 57 | + |
| 58 | + PT_BEGIN(pt); |
| 59 | + |
| 60 | + // Wait for motor simulation process to be ready. |
| 61 | + while (pbdrv_init_busy()) { |
| 62 | + PT_YIELD(pt); |
| 63 | + } |
| 64 | + |
| 65 | + // Start motor control process manually. |
| 66 | + pbio_motor_process_start(); |
| 67 | + |
| 68 | + // Initialize the servos. |
| 69 | + tt_uint_op(pbio_servo_get_servo(PBIO_PORT_ID_A, &srv_left), ==, PBIO_SUCCESS); |
| 70 | + tt_uint_op(pbio_servo_setup(srv_left, PBIO_DIRECTION_COUNTERCLOCKWISE, 1000, true, 0), ==, PBIO_SUCCESS); |
| 71 | + tt_uint_op(pbio_servo_get_servo(PBIO_PORT_ID_B, &srv_right), ==, PBIO_SUCCESS); |
| 72 | + tt_uint_op(pbio_servo_setup(srv_right, PBIO_DIRECTION_CLOCKWISE, 1000, true, 0), ==, PBIO_SUCCESS); |
| 73 | + |
| 74 | + // Set up the drivebase. |
| 75 | + tt_uint_op(pbio_drivebase_get_drivebase(&db, srv_left, srv_right, 56000, 112000), ==, PBIO_SUCCESS); |
| 76 | + tt_uint_op(pbio_drivebase_get_state_user(db, &drive_distance, &drive_speed, &turn_angle_start, &turn_rate), ==, PBIO_SUCCESS); |
| 77 | + tt_uint_op(pbio_drivebase_is_stalled(db, &stalled, &stall_duration), ==, PBIO_SUCCESS); |
| 78 | + tt_want(!stalled); |
| 79 | + |
| 80 | + // Get current settings and change them. |
| 81 | + tt_uint_op(pbio_drivebase_get_drive_settings(db, |
| 82 | + &drive_speed, |
| 83 | + &drive_acceleration, |
| 84 | + &drive_deceleration, |
| 85 | + &turn_rate, |
| 86 | + &turn_acceleration, |
| 87 | + &turn_deceleration), ==, PBIO_SUCCESS); |
| 88 | + |
| 89 | + // Try to set invalid settings. |
| 90 | + tt_uint_op(pbio_drivebase_set_drive_settings(db, |
| 91 | + drive_speed, |
| 92 | + drive_acceleration * 100, |
| 93 | + drive_deceleration, |
| 94 | + turn_rate, |
| 95 | + turn_acceleration, |
| 96 | + turn_deceleration), ==, PBIO_ERROR_INVALID_ARG); |
| 97 | + |
| 98 | + // Try to set invalid settings. |
| 99 | + tt_uint_op(pbio_drivebase_set_drive_settings(db, |
| 100 | + 200, |
| 101 | + drive_acceleration * 2, |
| 102 | + drive_deceleration, |
| 103 | + turn_rate, |
| 104 | + turn_acceleration, |
| 105 | + turn_deceleration), ==, PBIO_SUCCESS); |
| 106 | + |
| 107 | + // Drive straight for a distance and coast smart. |
| 108 | + tt_uint_op(pbio_drivebase_drive_straight(db, 1000, PBIO_CONTROL_ON_COMPLETION_COAST_SMART), ==, PBIO_SUCCESS); |
| 109 | + pbio_test_sleep_until(pbio_drivebase_is_done(db)); |
| 110 | + |
| 111 | + // Target should be stationary and close to target. |
| 112 | + pbio_test_sleep_ms(&timer, 200); |
| 113 | + tt_uint_op(pbio_drivebase_get_state_user(db, &drive_distance, &drive_speed, &turn_angle, &turn_rate), ==, PBIO_SUCCESS); |
| 114 | + tt_want(pbio_test_int_is_close(drive_distance, 1000, 30)); |
| 115 | + tt_want(pbio_test_int_is_close(drive_speed, 0, 50)); |
| 116 | + tt_want(pbio_test_int_is_close(turn_angle, turn_angle_start, 5)); |
| 117 | + tt_want(pbio_test_int_is_close(turn_rate, 0, 10)); |
| 118 | + |
| 119 | + // Drive straight for a distance and keep driving. |
| 120 | + tt_uint_op(pbio_drivebase_drive_straight(db, 1000, PBIO_CONTROL_ON_COMPLETION_CONTINUE), ==, PBIO_SUCCESS); |
| 121 | + pbio_test_sleep_until(pbio_drivebase_is_done(db)); |
| 122 | + |
| 123 | + // Target should be moving at given speed and close to target. |
| 124 | + tt_uint_op(pbio_drivebase_get_state_user(db, &drive_distance, &drive_speed, &turn_angle, &turn_rate), ==, PBIO_SUCCESS); |
| 125 | + tt_want(pbio_test_int_is_close(drive_distance, 2000, 20)); |
| 126 | + tt_want(pbio_test_int_is_close(drive_speed, 200, 5)); |
| 127 | + tt_want(pbio_test_int_is_close(turn_angle, turn_angle_start, 5)); |
| 128 | + tt_want(pbio_test_int_is_close(turn_rate, 0, 5)); |
| 129 | + |
| 130 | + // Test driving/turning forever, maintaining the speed we are already on. |
| 131 | + tt_uint_op(pbio_drivebase_drive_forever(db, 200, 90), ==, PBIO_SUCCESS); |
| 132 | + tt_uint_op(pbio_drivebase_get_state_user(db, &drive_distance, &drive_speed, &turn_angle, &turn_rate), ==, PBIO_SUCCESS); |
| 133 | + tt_want(pbio_test_int_is_close(drive_distance, 2000, 20)); |
| 134 | + tt_want(pbio_test_int_is_close(drive_speed, 200, 5)); |
| 135 | + tt_want(pbio_test_int_is_close(turn_angle, turn_angle_start, 5)); |
| 136 | + tt_want(pbio_test_int_is_close(turn_rate, 0, 5)); |
| 137 | + |
| 138 | + // After a while, the target speed/rate should be reached. |
| 139 | + pbio_test_sleep_ms(&timer, 2000); |
| 140 | + tt_uint_op(pbio_drivebase_get_state_user(db, &drive_distance, &drive_speed, &turn_angle, &turn_rate), ==, PBIO_SUCCESS); |
| 141 | + tt_want(pbio_test_int_is_close(drive_speed, 200, 5)); |
| 142 | + tt_want(pbio_test_int_is_close(turn_rate, 90, 5)); |
| 143 | + |
| 144 | +end: |
| 145 | + |
| 146 | + PT_END(pt); |
| 147 | +} |
| 148 | + |
| 149 | +struct testcase_t pbio_drivebase_tests[] = { |
| 150 | + PBIO_PT_THREAD_TEST(test_drivebase_basics), |
| 151 | + END_OF_TESTCASES |
| 152 | +}; |
0 commit comments