Skip to content

Commit

Permalink
🐛 Fix Global Gyro Constructors
Browse files Browse the repository at this point in the history
Summary:
The delay in the `adi_gyro_init()` function called by the `pros::ADIGyro` ctor caused segfaults when creating
a gyro object globally, since the scheduler hasn't started at that point.

Closes T640.

Test Plan:
[x] Compiles
[x] Run test code with a global gyro ctor and observe if/when the VexOS delay happens.
Test code:
```
#include "main.h"

pros::ADIGyro global_gyro ('a');

void opcontrol() {
	while (true) {
		pros::lcd::print(1, "%f", global_gyro.get_value());

		pros::delay(20);
	}
}
```
This displays `-32768.000000` for the 1300ms calibration time, then starts properly printing. For now we should just include a warning about this to users and encourage them to declare gyroscopes in a local scope, but this could be a good application for something along the lines of T635.

Reviewers: O1 The Ori, berman5

Reviewed By: O1 The Ori, berman5

Subscribers: berman5

Tags: #zorp

Maniphest Tasks: T640

Differential Revision: https://phabricator.purduesigbots.com/D201
  • Loading branch information
baylessj committed Sep 24, 2018
1 parent 69d15bd commit 8eaf135
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
6 changes: 3 additions & 3 deletions include/pros/adi.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,11 @@ double adi_gyro_get(adi_gyro_t gyro);

/**
* Initializes a gyroscope on the given port. If the given port has not
* previously been configured as a gyro, then this function starts a 1 second
* previously been configured as a gyro, then this function starts a 1300 ms
* calibration period.
*
* If calibration is required, it is highly recommended that this function be
* called from initialize when the robot is stationary.
* It is highly recommended that this function be called from initialize() when
* the robot is stationary to ensure proper calibration.
*
* This function uses the following values of errno when an error state is
* reached:
Expand Down
9 changes: 6 additions & 3 deletions include/pros/adi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,14 @@ class ADIGyro : private ADIPort {
public:
/**
* Initializes a gyroscope on the given port. If the given port has not
* previously been configured as a gyro, then this function starts a 1 second
* previously been configured as a gyro, then this function starts a 1300ms
* calibration period.
*
* If calibration is required, it is highly recommended that this function be
* called from initialize when the robot is stationary.
* It is highly recommended that an ADIGyro object be created in initialize()
* when the robot is stationary to ensure proper calibration. If an ADIGyro
* object is declared at the global scope, a hardcoded 1300ms delay at the
* beginning of initialize will be necessary to ensure that the gyro's
* returned values are correct at the beginning of autonomous/opcontrol.
*
* This function uses the following values of errno when an error state is
* reached:
Expand Down
12 changes: 10 additions & 2 deletions src/devices/vdml_adi.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

#define NUM_MAX_TWOWIRE 4

// Theoretical calibration time is 1024ms, but in practice this seemed to be the
// actual time that it takes.
#define GYRO_CALIBRATION_TIME 1300

typedef union adi_data {
struct {
int32_t calib;
Expand Down Expand Up @@ -373,8 +377,12 @@ adi_gyro_t adi_gyro_init(uint8_t port, double multiplier) {
}

int status = _adi_port_set_config(port, E_ADI_LEGACY_GYRO);
delay(1300); // Actual calibration time is 1024ms, but in practice this seemed
// to be the bare minimum time it takes
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)
// If the scheduler is currently running (meaning that this is not called
// from a global constructor, for example) then delay for the duration of
// the calibration time in VexOS.
delay(GYRO_CALIBRATION_TIME);

if (status)
return port;
else
Expand Down

0 comments on commit 8eaf135

Please sign in to comment.