EV3 C-code for third party devices (I2C)

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
lvoc
Posts: 38
Joined: 10 Sep 2013, 13:34

Re: EV3 C-code for third party devices (I2C)

Post by lvoc »

Yes, I want to figure why the compiler is trying to use or generate lms2012.o. In a C program you end up including many files (.h) that only have function/constant definitions (as lms2012.h). But, it does not mean that there will be a need to generate an object file (.o). My guess is that there is something that you set (or added) that is directing the compiler to want lms2012.o
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 C-code for third party devices (I2C)

Post by HaWe »

hi,
now the reason is both incomprehensible and embarrassing:

in the beginning I had copied all ev3_*.c files to the project manager window by mouse (mark + copy), and by mistake I accidentally had also copied the lms2012.h file although I know that it must not.
I had observed this very quickly already some days ago, clicked at lms2012.h and then chose "remove". It dissappeared from that window, and I closed it.
But the error remained the same all the time.

Now checking the project manager window again, the lms2012.h file appeared again - I removed it again, closed the window, compiled,...: still the same error. :?

Again checking the project manager window now, the lms2012.h file again was listed :o - I removed it again, closed the window, compiled - and now it's fine. 8-)

So the lms2012.h file seems to have not been removed permanently, although initially it had disappeared out of the window, that is the incomprehensible part. :shock:
The embarrassing part is that I didn't have checked it before by myself, but I didn't I come up with the idea that my file remove action may had failed. :oops:

I realy hope that this project manager thing could be sacrificed one day completely for the pure API header functions, that's way too intransparent IMO...

I then exchanged sleep by msleep for having quick 5ms loops

Code: Select all

inline void msleep(unsigned int msec) {usleep(msec*1000);}
now also this thing is fine.

so this is the current version:

Code: Select all

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include "lms2012.h"
#include "ev3_button.h"
#include "ev3_lcd.h"



//Runtime constants


//Global variables and constants used by the sensor handling functions

inline void msleep(unsigned int msec) {usleep(msec*1000);}

IIC *pXgl;

//***************************************************************************************
// Microinfinity XG1300L IMU settings

const int  XGL_PACKET_SIZE = 10; // 2(gyyro angle)+2(gyro rate)+2(acc x)+2(acc y)+2(acc z)
const char XGL_PORT = 0x3;       // The ports are designated as XGL_PORT_NUMBER-1
int        xgl_device_file;
//***************************************************************************************



int init_xg1300l_gyro()
{
    IICDAT IicDat;
    char buf[120];

    //Open the device xgl_device_file
    if((xgl_device_file = open(IIC_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)   {
      sprintf(buf, "Failed to open device");
      LcdText(1, 0, 60, buf);
      return 0;
    }

    pXgl = (IIC*)mmap(0, sizeof(IIC), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, xgl_device_file, 0);

    if (pXgl == MAP_FAILED)  {
      sprintf(buf, "Map failed");
      LcdText(1, 0, 60, buf);
      return 0;
    }

    //Setup IIC to read 2 packets
    IicDat.Port = XGL_PORT;
    IicDat.Time = 0;
    IicDat.Repeat = 0;
    IicDat.RdLng = XGL_PACKET_SIZE;
    IicDat.WrLng = 2;

    // Set the device I2C address
    IicDat.WrData[0] = 0x01;

    // Specify the register that will be read (0x42 = angle)
    IicDat.WrData[1] = 0x42;

    // Setup I2C comunication
    ioctl(xgl_device_file,IIC_SETUP,&IicDat);
    sprintf(buf,"Device is ready");
    LcdText(1, 0, 60, buf);

    return 1;
}

void get_xg1300l_gyro(float *angle, float *rate, float *acc_x, float *acc_y, float *acc_z)
{
    //Compute angle, angular rate and accelerations

    *acc_z = (pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][0]*256+pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][1])/100.0;
    *acc_y = (pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][2]*256+pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][3])/100.0;
    *acc_x = (pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][4]*256+pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][5])/100.0;
    *rate  = (pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][6]*256+pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][7])/100.0;
    *angle = (pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][8]*256+pXgl->Raw[XGL_PORT][pXgl->Actual[XGL_PORT]][9])/100.0;
}

void close_xg1300l_gyro()     //Close the device xgl_device_file
{
   char buf[120];

   sprintf(buf,"Closing device\n");
   LcdText(1, 0, 60, buf);

   close(xgl_device_file);
}

int main()
{
  char  buf[120];

  float angle;
  float rate;
  float acc_x;
  float acc_y;
  float acc_z;

  ButtonLedInit();
  LcdInit();
  LcdClean();

  if(!init_xg1300l_gyro())
      return -1;

  LcdClean();

  while(1)
   {
    get_xg1300l_gyro(&angle, &rate, &acc_x, &acc_y, &acc_z);

    sprintf(buf,"Angle= %6.2f deg  ", angle);
    LcdText(1, 0, 10, buf);
    sprintf(buf,"Rate = %6.2f deg/sec  ", rate);
    LcdText(1, 0, 20, buf);
    sprintf(buf,"AccX = %6.2f G   ", acc_x);
    LcdText(1, 0, 40, buf);
    sprintf(buf,"AccY = %6.2f G   ", acc_y);
    LcdText(1, 0, 50, buf);
    sprintf(buf,"AccZ = %6.2f G  ", acc_z);
    LcdText(1, 0, 60, buf);
     if(ButtonWaitForAnyPress(100) == BUTTON_ID_ESCAPE)
         break;
    msleep(5);
  }

  close_xg1300l_gyro();
  LcdExit();
  ButtonLedExit();
  return 1;
}

Now odometry is useless for me because my tank treads often are spinning and slipping.
So my hope was to use the accX and accY values for dead reckonning instead of odometry (by double integration over time: Δs = 1/2 * Δt² * a),
but the accelerometer is way too sensitive to inclined robot positions:
it already shows at standstill (mounted rather horizontally on the robot) -2m/s² for both x and y constantly.
Imagine what happens if the robot is driving upon uneven ground or even on inclined planes.

Now this is a mathematical challenge one probably would need a complex EKF model for, but for this task I must surely resign. :|
But I'm looking forward now to try to use the NXT US sensors for some experiments.
totokan
Posts: 45
Joined: 28 Sep 2013, 04:22

Re: EV3 C-code for third party devices (I2C)

Post by totokan »

There seems to be a glitch in the project manager, sometimes it looks like it removes things but actually doesn't. Also, it has weird highlighting issues, if you right click on an item it doesn't actually highlight it, so it works differently from what one would expect.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 C-code for third party devices (I2C)

Post by HaWe »

strange -
after a while I tried to compile this code from above again and now get lots of errors - what's possibly wrong?
In file included from ev3_button.h:46,
from XG1300L.c:8:
ev3_constants.h:52:1: warning: "LCD_WIDTH" redefined
In file included from XG1300L.c:7:
lms2012.h:196:1: warning: this is the location of the previous definition
In file included from ev3_button.h:46,
from XG1300L.c:8:
ev3_constants.h:53:1: warning: "LCD_HEIGHT" redefined
In file included from XG1300L.c:7:
lms2012.h:197:1: warning: this is the location of the previous definition
In file included from ev3_button.h:46,
from XG1300L.c:8:
ev3_constants.h:54:1: warning: "TOPLINE_HEIGHT" redefined
In file included from XG1300L.c:7:
lms2012.h:198:1: warning: this is the location of the previous definition
In file included from ev3_button.h:46,
from XG1300L.c:8:
ev3_constants.h:330:1: warning: "FILENAME_SIZE" redefined
In file included from XG1300L.c:7:
lms2012.h:367:1: warning: this is the location of the previous definition
In file included from XG1300L.c:9:
ev3_lcd.h:52:1: warning: "LCD_BUFFER_SIZE" redefined
In file included from XG1300L.c:7:
lms2012.h:1237:1: warning: this is the location of the previous definition
In file included from XG1300L.c:9:
ev3_lcd.h:54: error: redefinition of typedef 'IMGDATA'
lmstypes.h:69: error: previous declaration of 'IMGDATA' was here
ev3_lcd.h:55: error: redefinition of typedef 'IP'
lmstypes.h:74: error: previous declaration of 'IP' was here
make: *** [XG1300L] Error 1
the project manager lists:
ev3_timer.c / button.c / command.c / lcd.c / output.c / sound.c
totokan
Posts: 45
Joined: 28 Sep 2013, 04:22

Re: EV3 C-code for third party devices (I2C)

Post by totokan »

I'll take a look and see. A bit busy this week, hopefully will get a shot by the weekend...
ulmeco
Posts: 7
Joined: 23 Jan 2014, 20:51

Re: EV3 C-code for third party devices (I2C)

Post by ulmeco »

doc-helmut wrote:strange -
after a while I tried to compile this code from above again and now get lots of errors - what's possibly wrong?
I'm guessing you're still including both "lms2012.h" and "ev3_*.h" as seen in the code snippet above. The compiler doesn't like that some #defines are defined twice, like for example LCD_WIDTH, which gives warnings. The errors are very similar, but are for typedefs instead of defines.

If you need to include all of lms2012.h and lmstypes.h, you have a few options - the absolute easiest and fastest option is to just comment out the offending typedefs from lmstypes.h (IMGDATA and IP), as they're basically the same in ev3_lcd.h. Or you could rename them in ev3_lcd.h and ev3_lcd.c.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 C-code for third party devices (I2C)

Post by HaWe »

I have some hacks of the ev3_* files by Lauro which once have worked with lms2012 but don't do any longer though, strangely,
and I also tried the original ev3_* files from BCC/C with a hack of the lms2012.h by totokan, but they also don't compile and are showing lots of errors

Also the redefinitions just gave compiler warnings so far but always could be compiled nevertheless, but now there must be a real (obscured) fatal error somewhere...
Image
lvoc
Posts: 38
Joined: 10 Sep 2013, 13:34

Re: EV3 C-code for third party devices (I2C)

Post by lvoc »

A little while ago I wrote a program that reads data from the gyro and displays it in the LCD. This program could be used as starting point. This program uses a slightly modified LVM library for handling the LCD. You can find the program at:
http://www.robotnav.com/sensor-and-lcd/

When working with the LCD, you will need to compile a few other files as explained here:
http://www.robotnav.com/lcd/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 C-code for third party devices (I2C)

Post by HaWe »

RobotNavigation@http://www.robotnav.com/lcd/ wrote:For windows users, I recommend reading our tutorial on creating and compiling projects with Bricxcc.
Can you pls help me where to find this tutorial?
lvoc
Posts: 38
Joined: 10 Sep 2013, 13:34

Re: EV3 C-code for third party devices (I2C)

Post by lvoc »

The instructions are the same used to compile programs that use several source files. You can find them at:
http://www.robotnav.com/download_instructions/#windows
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 5 guests