Environment
A V5 Brain with VEXos 1.1.0, a rotation sensor, and a SD card
Problem
If you read a file in the code, reverse the rotation sensor, then read the position value of that sensor, the return value is incorrect.
Which means, for example, if you run the code below, every time you start the program, you may get a value starting from 2875 or 33125 ( 36000 - x
or x
).
Test code in PROS
#include "main.h"
extern "C" {
typedef void FIL;
FIL *vexFileOpen(const char *filename, const char *mode );
int32_t vexAbsEncPositionGet( uint32_t index );
void vexAbsEncReverseFlagSet( uint32_t index, bool value );
};
void initialize() {
pros::lcd::initialize();
vexFileOpen("/usd/anything1.config", "r");
int port = 10;
vexAbsEncReverseFlagSet(port-1, true);
while (true) {
pros::lcd::print(0, "%d", vexAbsEncPositionGet(port-1)); // get 1512 or 34488
pros::delay(20);
}
}
Test code in VEXcode
#include "vex.h"
using namespace vex;
extern "C" {
typedef void FIL;
FIL *vexFileOpen(const char *filename, const char *mode );
int32_t vexAbsEncPositionGet( uint32_t index );
void vexAbsEncReverseFlagSet( uint32_t index, bool value );
};
int main() {
vexFileOpen("/usd/anything1.config", "r");
wait(10, msec); // bruh
int port = 10;
vexAbsEncReverseFlagSet(port-1, true);
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3, 3);
Brain.Screen.print("%d", vexAbsEncPositionGet(port-1));
wait(200, msec);
}
}
More Details
Run the code below with or without vexFileOpen
and a 10msec delay after the line
// skip...
void initialize() {
pros::lcd::initialize();
vexFileOpen("/usd/anything1.config", "r"); // test subject
pros::delay(10); // test subject
int port = 10;
vexAbsEncReverseFlagSet(port-1, true);
while (true) {
pros::lcd::print(0, "%d", vexAbsEncPositionGet(port-1)); // get 1512 or 34488
pros::delay(20);
}
}
Result:
vexFileOpen and delay
SD Card inserted: 34488 (always)
Without SD Card: 1512 (always)
vexFileOpen and no delay
SD Card inserted: 1512 or 34488
Without SD Card: 1512 (always)
no vexFileOpen and < 10msec delay
SD Card inserted: 34488 (always)
Without SD Card: 34488 (always)
no vexFileOpen and 50msec delay
SD Card inserted: 1512 (always)
Without SD Card: 1512 (always)
Workaround
Adding a 50 millisecond delay at the beginning.
In PROS:
void initialize() {
pros::lcd::initialize();
pros::delay(50);
// your code
}
In VEXcode:
int main() {
wait(50, msec);
// your code
}