First time install of the http://wit-motion.com IMU on Teensy 4.1

laptophead

Well-known member
This IMU is serial interfaced, and does a lot of calculations and corrections internally, so I was attracted to it.

The manufacturer provides a small library that compiles fine for Ardu mega, but not for teensy...

I get
Arduino: 1.8.13 (Mac OS X), TD: 1.53, Board: "Teensy 4.1, Serial, 600 MHz, Faster, US English"











In file included from /Users/mitchanderson/Documents/Arduino/libraries/JY901SerialMega2560/JY901.cpp:1:0:
/Users/mitchanderson/Documents/Arduino/libraries/JY901SerialMega2560/JY901.h:8:17: error: expected unqualified-id before numeric constant
#define BAUD 0x04
^
/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/cores/teensy4/imxrt.h:6769:20: note: in expansion of macro 'BAUD'
volatile uint32_t BAUD;
^
Error compiling for board Teensy 4.1.

Should I modify something in the CCP or .h ?

Thanks a lot.

View attachment JY901.cppView attachment JY901.hView attachment JY901Serial.ino
 
That means the code is asking the compiler to do this:
volatile uint32_t 0x04

...which means "create a variable called 0x04", rather than "create a variable called baud and set it to 0x04". So it seems like a typo...

Can you edit your post so the code is inside code tags instead of attachments?
 
The problem is that teensy4/imxrt.h and JY901.h both use the name BAUD but for different uses. One way to resolve it is to edit the JY901 library (JY901.cpp and JY901.h) and change every occurrence of "BAUD" to something else - e.g. "JY9_BAUD" - so that the two don't conflict.

Pete
 
Said and done,
I did replace it only in the .h file , could not find it in the .cpp. (BAUD does not appear)

It did compile, and I uploaded.

However it returns no data. So the IMU does not like something....

Serial output is:
Time:200-0-0 0:0:0.00
Acc:0.00 0.00 0.00
Gyro:0.00 0.00 0.00
Angle:0.00 0.00 0.00
Mag:0 0 0
Pressure:0 0.00
DStatus:0 0 0 0
Longitude:0Deg0.00m Lattitude:0Deg0.00m
GPSHeight:0.00m GPSYaw:0.00Deg GPSV:0.00km/h
SN:0 PDOP:0.00 HDOP:0.00 VDOP:0.00


The contents of the .h is now

Code:
#ifndef JY901_h
#define JY901_h

#define SAVE 			0x00
#define CALSW 		0x01
#define RSW 			0x02
#define RRATE			0x03
#define JY9_BAUD 		0x04
#define AXOFFSET	0x05
#define AYOFFSET	0x06
#define AZOFFSET	0x07
#define GXOFFSET	0x08
#define GYOFFSET	0x09
#define GZOFFSET	0x0a
#define HXOFFSET	0x0b
#define HYOFFSET	0x0c
#define HZOFFSET	0x0d
#define D0MODE		0x0e
#define D1MODE		0x0f
#define D2MODE		0x10
#define D3MODE		0x11
#define D0PWMH		0x12
#define D1PWMH		0x13
#define D2PWMH		0x14
#define D3PWMH		0x15
#define D0PWMT		0x16
#define D1PWMT		0x17
#define D2PWMT		0x18
#define D3PWMT		0x19
#define IICADDR		0x1a
#define LEDOFF 		0x1b
#define GPSBAUD		0x1c

#define YYMM				0x30
#define DDHH				0x31
#define MMSS				0x32
#define MS					0x33
#define AX					0x34
#define AY					0x35
#define AZ					0x36
#define GX					0x37
#define GY					0x38
#define GZ					0x39
#define HX					0x3a
#define HY					0x3b
#define HZ					0x3c			
#define Roll				0x3d
#define Pitch				0x3e
#define Yaw					0x3f
#define TEMP				0x40
#define D0Status		0x41
#define D1Status		0x42
#define D2Status		0x43
#define D3Status		0x44
#define PressureL		0x45
#define PressureH		0x46
#define HeightL			0x47
#define HeightH			0x48
#define LonL				0x49
#define LonH				0x4a
#define LatL				0x4b
#define LatH				0x4c
#define GPSHeight   0x4d
#define GPSYAW      0x4e
#define GPSVL				0x4f
#define GPSVH				0x50
      
#define DIO_MODE_AIN 0
#define DIO_MODE_DIN 1
#define DIO_MODE_DOH 2
#define DIO_MODE_DOL 3
#define DIO_MODE_DOPWM 4
#define DIO_MODE_GPS 5		

struct STime
{
	unsigned char ucYear;
	unsigned char ucMonth;
	unsigned char ucDay;
	unsigned char ucHour;
	unsigned char ucMinute;
	unsigned char ucSecond;
	unsigned short usMiliSecond;
};
struct SAcc
{
	short a[3];
	short T;
};
struct SGyro
{
	short w[3];
	short T;
};
struct SAngle
{
	short Angle[3];
	short T;
};
struct SMag
{
	short h[3];
	short T;
};

struct SDStatus
{
	short sDStatus[4];
};

struct SPress
{
	long lPressure;
	long lAltitude;
};

struct SLonLat
{
	long lLon;
	long lLat;
};

struct SGPSV
{
	short sGPSHeight;
	short sGPSYaw;
	long lGPSVelocity;
};
struct SQuater
{
	short q0;
	short q1;
	short q2;
	short q3;
};
struct SSN
{
	short sSVNum;
	short sPDOP;
	short sHDOP;
	short sVDOP;
};
class CJY901 
{
  public: 
	struct STime		stcTime;
	struct SAcc 		stcAcc;
	struct SGyro 		stcGyro;
	struct SAngle 		stcAngle;
	struct SMag 		stcMag;
	struct SDStatus 	stcDStatus;
	struct SPress 		stcPress;
	struct SLonLat 		stcLonLat;
	struct SGPSV 		stcGPSV;
	struct SQuater		stcQuater;
	struct SSN 		stcSN;
	
    CJY901 (); 
	void StartIIC();
	void StartIIC(unsigned char ucAddr);
    void CopeSerialData(unsigned char ucData);
	short ReadWord(unsigned char ucAddr);
	void WriteWord(unsigned char ucAddr,short sData);
	void ReadData(unsigned char ucAddr,unsigned char ucLength,char chrData[]);
	void GetTime();
	void GetAcc();
	void GetGyro();
	void GetAngle();
	void GetMag();
	void GetPress();
	void GetDStatus();
	void GetLonLat();
	void GetGPSV();
	
  private: 
	unsigned char ucDevAddr; 
	void readRegisters(unsigned char deviceAddr,unsigned char addressToRead, unsigned char bytesToRead, char * dest);
	void writeRegister(unsigned char deviceAddr,unsigned char addressToWrite,unsigned char bytesToRead, char *dataToWrite);
};
extern CJY901 JY901;
#include <Wire.h>
#endif
 
Post a diagram (and/or a photo) of how you have the JY901 connected to the Teensy - perhaps you have the Tx and Rx pins the wrong way round?

Pete
 
Tried your recommended

Thanks a lot, Pete
I installed your library, but unfortunately I don't have a Ardu UNO

So I tried to make it work on Serial1 on my teensy 4.1

Code:
#include <JY901.h>
/*
Test on Uno R3.
JY901   UnoR3
TX <---> 0(Rx)
*/
void setup() 
{
	Serial.begin(9600);
 Serial1.begin(9600);
	JY901.attach(Serial1);
}

void loop() 
{
	JY901.receiveSerialData();


	//print received data. Data was received in serialEvent;
	Serial.print("Time:20");
	Serial.print(JY901.getTime("year"));
	Serial.print("-");
	Serial.print(JY901.getTime("month"));
	Serial.print("-");
	Serial.print(JY901.getTime("day"));
	Serial.print(" ");
	Serial.print(JY901.getTime("hour"));
	Serial.print(":");
	Serial.print(JY901.getTime("minute"));
	Serial.print(":");
	Serial.println((float)JY901.getTime("second")+(float)JY901.getTime("milisecond")/1000);

							 
	Serial.print("Acc:");
	Serial.print(JY901.getAccX());
	Serial.print(" ");
	Serial.print(JY901.getAccY());
	Serial.print(" ");
	Serial.print(JY901.getAccZ());
	Serial.print("\n");

	
	Serial.print("Gyro:");
	Serial.print(JY901.getGyroX());
	Serial.print(" ");
	Serial.print(JY901.getGyroY());
	Serial.print(" ");
	Serial.print(JY901.getGyroZ());
	Serial.print("\n");
	
	Serial.print("Mag:");
	Serial.print(JY901.getMagX());
	Serial.print(" ");
	Serial.print(JY901.getMagY());
	Serial.print(" ");
	Serial.print(JY901.getMagZ());
	Serial.print("\n");

	Serial.print("Angle:");
	Serial.print(JY901.getRoll());
	Serial.print(" ");
	Serial.print(JY901.getPitch());
	Serial.print(" ");
	Serial.print(JY901.getYaw());
	Serial.print("\n");

	Serial.print("Pressure:");
	Serial.println(JY901.getPressure());
	Serial.print("Altitude:");
	Serial.println(JY901.getAltitude()/100.0);

	
	Serial.print("DStatus:");
	Serial.print(JY901.getD0Status());
	Serial.print(" ");
	Serial.print(JY901.getD1Status());
	Serial.print(" ");
	Serial.print(JY901.getD2Status());
	Serial.print(" ");
	Serial.print(JY901.getD3Status());
	Serial.print("\n");

	
	Serial.print("Longitude:");
	Serial.print(JY901.getLon()/10000000);
	Serial.print("Deg");
	Serial.print((double)(JY901.getLon() % 10000000)/1e5);
	Serial.println("m");

	Serial.print("Lattitude:");
	Serial.print(JY901.getLat()/10000000);
	Serial.print("Deg");
	Serial.print((double)(JY901.getLat() % 10000000)/1e5);
	Serial.println("m");

	
	Serial.print("GPSHeight:");
	Serial.print(JY901.getGPSH());
	Serial.println("m");
	Serial.print("GPSYaw:");
	Serial.print(JY901.getGPSY());
	Serial.println("Deg:");
	Serial.print("GPSV:");
	Serial.print(JY901.getGPSV());
	Serial.println("km/h");

	
	Serial.println("");

	delay(500);

}

I just get zeros.

Hardware I think is fine. The IMU Spits some data periodically on the TX wire

But what a stupid example.
Who wants to talk to the IMU and serial print on the same port?
 

Attachments

  • IMG_1842.jpg
    IMG_1842.jpg
    120.5 KB · Views: 69
  • IMG_1843.jpg
    IMG_1843.jpg
    111.4 KB · Views: 103
Last edited:
Either of those libraries should work (I found where your example reads serial DUH! :) ).
So, it's a hardware problem!
Only thing I notice in the photo is that you appear to be powering the JY901 from the Teensy Vin pin. This may be powering the JY901 with 5V which could be a problem if the Tx pin is also at 5V. Try powering the JY901 from the 3V3 pin.

Pete
 
Good observation, I moved the power to the 3.3 and now the signal is at 3.3V

Still no reading,
I wonder if I formulated the read from Ser 1 correctly...

#include <JY901.h>
/*
Test on Uno R3.
JY901 UnoR3
TX <---> 0(Rx)
*/
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
JY901.attach(Serial1);
}
 
Your code looks correct as long as the JY901 is set for 9600 baud.
It is possible that having the JY901 Tx pin at 5V has damaged the Teensy Rx pin - the T4.1 is not 5V tolerant. Try this loopback test of the Serial1 port. Put a jumper wire between Teensy pins 0 and 1.

Code:
// Simple loopback test of Serial1

void setup(void)
{
  Serial.begin(9600);
  while(!Serial);
  delay(1000);
  Serial1.begin(9600);
}

void loop(void)
{
  Serial1.println("Hello");
  Serial.print("Waiting for Serial1 response ... ");
  delay(100);
  while(Serial1.available() < 6);
  while(Serial1.available())Serial.write(Serial1.read());
  delay(1000);
}

It should repeatedly print this
Code:
Waiting for Serial1 response ... Hello
If it doesn't receive anything it will hang with just this
Code:
Waiting for Serial1 response ...

Pete
 
Pete, thats a smart way to see whats coming,

Im out of my knowledge by now,
Tried it and I get

�Waiting for Serial1 response ... �ɼ������0�@�LM��i� �!��um ������|������9�|����Waiting for Serial1 response ..
 
That suggests that the JY901 isn't at 9600 baud.
Change this
Code:
  Serial1.begin(9600);
To try the other baud rates: 2400, 4800, 19200 and 38400.

Pete
 
Tried all the way to the 112500, still getting garbage

There is some software from the manufacturer that I can set up the Baud.
Got to go now, travel on business. Will mess with it on fri

Thanks Pete,
Mitch
 
Hang on. The JY901 has nothing to do with the loopback test! My bad. The Serial1.begin sets the baud rate of the loopback. The JY901 isn't there.
It is not a good sign that the loopback doesn't work. It suggests that the Rx pin isn't working.

One sanity check you can try on Friday would be to do the loopback on another Serial port. I've reworked the code slightly to use Serial2, which uses pins 7 and 8.
Both Serial1 and Serial2 work with this code on a T4.1
Code:
// Simple loopback test of a serial port

// Choose which serial port to test
#define LOOPBACK_PORT Serial2

void setup(void)
{
  Serial.begin(9600);
  while(!Serial);
  delay(1000);
  LOOPBACK_PORT.begin(9600);
}

void loop(void)
{
  LOOPBACK_PORT.println("Hello");
  Serial.print("Waiting for Serial response ... ");
  delay(100);
  while(LOOPBACK_PORT.available() < 6);
  while(LOOPBACK_PORT.available())Serial.write(LOOPBACK_PORT.read());
  delay(1000);
}

Pete
 
P.S. If the loopback on Serial2 works, you can change the JY901 code to use that port instead of Serial1.

Pete
 
Pete,
I moved it to Com 8 and I get the same...
I appreciate your help, but I decided to move to another IMU the Adafruit_BNO055. Works great, and there is support.

Thanks again
 
I'm using the WT901B with Teensy 4.0, using the IIC port, pins 18 and 19 and its sample source code for IIC. The same with different models of Arduino. Did not test using serial port
 
Back
Top