Microinfinity XG1300L gyro+acceleration sensor

HomeForumsMonoBrick EV3 FirmwareMicroinfinity XG1300L gyro+acceleration sensor

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #4201
    Author Image
    Helmut Wunder
    Participant

    hey,
    how could one use the Microinfinity XG1300L gyro+acceleration sensor to first reset and then display angle and acceleration values on the ev3 screen similar to the following NXC program?

    /*
     * Microinfinity CO. LTD.
     * Test program for the XGL1300L device
     * This program displays the complete data package from the device
     * it resets the sensor when the user press the left button
     * it changes the accelerometer SF when the user press the right button
    */
    //Define constants
    #define XGL_PORT S1
    #define XGL_ADDR 0x02
    #define XGL_DATA_REG  0x42
    #define XGL_RESET_REG  0x60
    #define XGL_ACC_SF_REG  0x61
    #define XGL_DATA_PACKET 10
    #define XGL_COMMAND 2
    #define XGL_TIME_OUT 500
    
    //XGL1300L sensor data packet
    struct XGLpacket
    {
    	short mAng;
    	short mRate;
    	short mAccX;
    	short mAccY;
    	short mAccZ;
    };
    
    //Reset XGL1300L sensor
    void XglReset()
    {
    	byte cmd[XGL_COMMAND];
    	byte n_read=0;
    	ArrayBuild(cmd,XGL_ADDR, XGL_RESET_REG);
    	while (I2CStatus(XGL_PORT, n_read) ==  STAT_COMM_PENDING);
    	I2CWrite(XGL_PORT,0,cmd);
    }
    
    //Change scale factor, the accSf parameter can be
    // 0 -> +/- 2G
    // 1 -> +/- 4G
    // 2 -> +/- 8g
    void XglAccSF(byte accSf)
    {
    	byte cmd[XGL_COMMAND];
    	byte n_read=0;
    	byte register= XGL_ACC_SF_REG+accSf;
    	ArrayBuild(cmd,XGL_ADDR, register);
    	while (I2CStatus(XGL_PORT, n_read) ==  STAT_COMM_PENDING);
    	I2CWrite(XGL_PORT,0,cmd);
    }
    
    //Reads the full packet from XGL1300L
    void XglReadPacket(XGLpacket &XglData)
    {
    	byte data[XGL_DATA_PACKET];
    	byte cmd[XGL_COMMAND];
    	byte count=XGL_DATA_PACKET;
    	byte n_read=0;
    	ArrayBuild(cmd,XGL_ADDR, XGL_DATA_REG);
    	while (I2CStatus(XGL_PORT, n_read) ==  STAT_COMM_PENDING);
    	if (I2CBytes(XGL_PORT, cmd, count, data))
    	{//Assemble data
    		XglData.mAng = data[0] + data[1]*256;
    		XglData.mRate = data[2] + data[3]*256;
    		XglData.mAccX = data[4] + data[5]*256;
    		XglData.mAccY = data[6] + data[7]*256;
    		XglData.mAccZ = data[8] + data[9]*256;
    	}
    }
    
    task main()
    {
    	XGLpacket xgl;
    	string msg;
    	byte acc_sf=0;
    	ReadButtonType rbArgs;
    
    	//Initialize system
    	SetSensorLowspeed(XGL_PORT);
    	//Resets sensor and waits for hardware to settle
    	XglReset();
    	Wait(XGL_TIME_OUT);
    
    	//Main loop
    	while (1)
    	{
    		ClearScreen();
    		XglReadPacket(xgl);
    		TextOut(0, LCD_LINE1,"<RESET / ACC_SF>", false);
    		//Print Angle  value
    		TextOut(0, LCD_LINE3,"ANGLE:");
    		NumOut(40, LCD_LINE3,xgl.mAng);
    		//Print Rate value
    		TextOut(0, LCD_LINE4,"RATE:");
    		NumOut(40, LCD_LINE4,xgl.mRate);
    		//Print Acc x value
    		TextOut(0, LCD_LINE5,"ACC_X:");
    		NumOut(40, LCD_LINE5,xgl.mAccX);
    		//Print Acc y value
    		TextOut(0, LCD_LINE6,"ACC_Y:");
    		NumOut(40, LCD_LINE6,xgl.mAccY);
    		//Print Acc z value
    		TextOut(0, LCD_LINE7,"ACC_Z:");
    		NumOut(40, LCD_LINE7,xgl.mAccZ);
    		
    		//Reset sensor if user press left key
    		rbArgs.Index = BTNLEFT;
    		SysReadButton(rbArgs);
    		if (rbArgs.Pressed)
    		{
    			XglReset();
    			TextOut(0, LCD_LINE2,"Reseting XGL ...", false);
    			Wait(XGL_TIME_OUT);
    		}
    
    		//Change accelerometer SF if user press right key
    		rbArgs.Index = BTNRIGHT;
    		SysReadButton(rbArgs);
    		if (rbArgs.Pressed)
    		{
    			acc_sf=(acc_sf==2)?0:acc_sf+1;
    			XglAccSF(acc_sf);
    			TextOut(0, LCD_LINE2,"Accel SF +/-");
    			NumOut(75, LCD_LINE2,2<<acc_sf);
    			Wait(XGL_TIME_OUT);
    		}
    		Wait(100);
    	}
    }
    #4204
    Author Image
    Anders Søborg
    Keymaster

    Hi Helmut

    I think the best approach is to have a look at the base class for I2C sensor. You can find it here… also have a look at the code for one of the sensors that use this class for example the HT Compass Sensor. Once you are done and have everything working please make a pull request so we can make this a part of the MonoBrick Firmware – let me know how it goes

    Anders

    #4206
    Author Image
    Helmut Wunder
    Participant

    thank you, I already had a look at the HT compass and different sensor source codes like HT accelerometer but unfortunately I don’t understand how i2c works with Mono commands.

    #4208
    Author Image
    Helmut Wunder
    Participant

    ps
    I must admit that I also do not understand how i2c commands work on NXC, the code above has been provided by Microinfinity itself. I was only able to use the published code by copy-and pasting them into my own NXC programs, so that’s what I actually need now for Mono. 🙂

    #4226
    Author Image
    Anders Søborg
    Keymaster

    Hi

    Ok – but It does not make sense for me to implement/test some new code – as I don’t own one of these…

    Anders

    #4237
    Author Image
    Helmut Wunder
    Participant

    I’d really like to contribute and give some code input if I got help transcribing the NXC i2c commands into Mono i2c commands
    ArrayBuild(); I2CStatus(); STAT_COMM_PENDING; I2CWrite(); and test the code and fix possible issues, and if the code once is finally fixed then it might – and should – be implemented. I’ll track and show the progress made by a youtube video.
    IMO the XG1300L IMU is worth while to be supported because it provides a never-seen accuracy and extremely low noise level or drift for navigaton and dead-reckoning.

    • This reply was modified 10 years, 6 months ago by Author ImageHelmut Wunder.
    #4239
    Author Image
    Helmut Wunder
    Participant

    ps
    something like printfxy(pixelX, pixelY, "formatstring", value (,value) (,value) (...) ) would be fine to have, too, for screen output as a substitute for NXC NumOut and TextOut

    • This reply was modified 10 years, 6 months ago by Author ImageHelmut Wunder.
    #4242
    Author Image
    Helmut Wunder
    Participant

    sry, can’t edit my post any more, I meant something like

    printfxy(0,10,"GyroAngle: %4d", gyro.Angle() );
    printfxy(0,20,"Acc XYZ:%4.2f%4.2f%4.2f", gyro.accX(), gyro.accY(), gyro.accZ() );
    

    is this possible ?

    #4249
    Author Image
    Anders Søborg
    Keymaster

    For LCD question please see here

    #4253
    Author Image
    Helmut Wunder
    Participant

    thanks for the LCD thing – now just the XG1300L gyro+acc i2c code is pending… 😉

    #4254
    Author Image
    Anders Søborg
    Keymaster

    Hi

    Well I won’t have time to look at it right now – but won’t this be a good opportunity to learn come C#?

    Anders

    #4256
    Author Image
    Helmut Wunder
    Participant

    haha, nice try 😀
    but actually: no. I daresay that I did a lot of things at the limits of NXC, but I ever failed trying to do low-level things like i2c, BT, or rs485 protocols.
    So i2c is actually not supposed to be an appropriate approach to learning C#…

    Other way round: having the transcoded NXC program from above to a C# source would make me deductively slowly step into understanding C# syntax rules 😎

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.

Posted in

Make a donation