Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/wt901 via SerialPIO #96

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions Firmware/LowLevel/lib/JY901_SERIAL/JY901.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@ CJY901 ::CJY901(SerialPIO *serial)
this->serial = serial;
}

void CJY901::begin(int baudrate) {
void CJY901::begin(unsigned long baudrate) {
serial->begin(baudrate);
delay(1000);
uint8_t unlock[] = {0xFF,0xF0,0xF0,0xF0,0xF0};
/*uint8_t unlock[] = {0xFF,0xF0,0xF0,0xF0,0xF0}; // undocumented / unknown?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't find this unlock code in any documentation. Only in a doubtful Amazon comment. So I disabled it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://forum.arduino.cc/t/help-calibrating-witmotion-inclinometer-through-ttl-with-mega2560/941394/1
This is what my Google-fu gave me, not sure if it is related thou

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is needed I remember sniffing the configuration of the GUI app to see how they do it because just sending config didn't work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comments!
After reading @FadeFx comment and link I immediately thought: "Could be an Auto-Baud detection sequence", but in-between tested it. It's not :-)
Will uncomment it again.

serial->write(unlock, 5);
serial->flush();
delay(10);*/
writeRegister(RSW, 0b0000000000010110); // Return data content
delay(10);
writeRegister(RSW, 0b0000000000010110);
writeRegister(RRATE, 0x08); // Return rate 0x06 = 10Hz (default), 0x08 = 50Hz, 0x09 = 100Hz
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMU_CYCLETIME is 20ms, so I reduced WT's return rate to 50Hz

delay(10);
writeRegister(RRATE, 0x09);
writeRegister(0x0b, 0x00); // X axis Magnetic bias
delay(10);
writeRegister(0x0b,0x00);
writeRegister(0x0c, 0x00); // Y axis Magnetic bias
delay(10);
writeRegister(0x0c,0x00);
writeRegister(0x0d, 0x00); // Z axis Magnetic bias
delay(10);
writeRegister(0x0d,0x00);
delay(10);




ucRxCnt = 0;
}
Expand All @@ -43,6 +40,7 @@ void CJY901 ::update()
if (ucRxBuffer[0] != 0x55)
{
ucRxCnt = 0;
commsError_ = true;
continue;
}
if (ucRxCnt < 11)
Expand Down Expand Up @@ -112,3 +110,12 @@ void CJY901::writeRegister(uint8_t address, uint16_t data) {
delay(100);

}

bool CJY901::commsError() {
bool hold = commsError_;
#ifdef WT901
hold |= serial->overflow();
#endif
commsError_ = false;
return hold;
}
14 changes: 8 additions & 6 deletions Firmware/LowLevel/lib/JY901_SERIAL/JY901.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,16 @@ class CJY901
struct SGPSV stcGPSV;

#ifdef WT901_INSTEAD_OF_SOUND
CJY901 (HardwareSerial *serial);
CJY901 (HardwareSerial *serial);
void begin(unsigned long baudrate = 9600UL);
#elif WT901
CJY901 (SerialPIO *serial);
#endif

void begin(int baudrate = 9600);
CJY901 (SerialPIO *serial);
void begin(unsigned long baudrate = 9700UL); // YES, 9700 baud! See SerialPIO timing issue https://github.com/earlephilhower/arduino-pico/issues/1276
#endif

// Call as often as possible to fetch data from serial
void update();

bool commsError(); // Get (and reset) communication error flag


private:
Expand All @@ -161,6 +162,7 @@ class CJY901
#endif
unsigned char ucRxBuffer[250];
unsigned char ucRxCnt = 0;
bool commsError_ = false; // Any kind of communication error

void writeRegister(uint8_t address, uint16_t data);
};
Expand Down
4 changes: 4 additions & 0 deletions Firmware/LowLevel/src/imu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ bool imu_read(float *acceleration_mss,float *gyro_rads,float *mag_uT);
// call once per loop, used to process serial or do sensor fusion or do nothing.
void imu_loop();

#if defined(WT901) || defined(WT901_INSTEAD_OF_SOUND)
bool imu_comms_error(); // get IMU communication error flag (and reset it)
#endif

#endif
5 changes: 5 additions & 0 deletions Firmware/LowLevel/src/imu/WT901_SERIAL/imu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ bool imu_read(float *acceleration_mss, float *gyro_rads, float *mag_uT)
void imu_loop()
{
IMU.update();
}

bool imu_comms_error()
{
return IMU.commsError();
}
22 changes: 14 additions & 8 deletions Firmware/LowLevel/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,10 @@ void sendConfigMessage(uint8_t pkt_type) {
ll_config.type = pkt_type;
ll_config.config_bitmask = config_bitmask;
ll_config.volume = 80; // FIXME: Adapt once nv_config or improve-sound got merged
strcpy(ll_config.language, "en"); // FIXME: Adapt once nv_config or improve-sound got merged
iso639_1 language = {'e', 'n'}; // FIXME: Adapt once nv_config or improve-sound got merged
for (unsigned int i = 0; i < sizeof(language); i++) {
ll_config.language[i] = language[i];
}
sendMessage(&ll_config, sizeof(struct ll_high_level_config));
}

Expand Down Expand Up @@ -632,17 +635,20 @@ void updateChargingEnabled() {

void updateNeopixel() {
led_blink_counter++;
// flash red on emergencies
if (emergency_latch && led_blink_counter & 0b10) {
p.neoPixelSetValue(0, 128, 0, 0, true);

if (emergency_latch && led_blink_counter & 0b100) { // slow blink on emergencies
p.neoPixelSetValue(0, 128, 0, 0, true); // 1/2 red
} else {
if (ROS_running) {
// Green, if ROS is running
p.neoPixelSetValue(0, 0, 255, 0, true);
p.neoPixelSetValue(0, 0, 255, 0, true); // green
} else {
// Yellow, if it's not running
p.neoPixelSetValue(0, 255, 50, 0, true);
p.neoPixelSetValue(0, 255, 50, 0, true); // yellow
}
#if defined(WT901) || defined(WT901_INSTEAD_OF_SOUND)
if (led_blink_counter & 0b10 && imu_comms_error()) { // fast blink on communication error (condition order matters -> short-circuit evaluation)
p.neoPixelSetValue(0, 255, 0, 255, true); // magenta
}
#endif
}
}

Expand Down