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

EV3 C-code for third party devices (I2C)

Post by lvoc »

I have been trying to use some older devices/sensors that use I2C communication, especially third party devices that are currently not support by the Ev3. I wrote a C-program that shows how to setup and reads data from a device that uses I2C (Microinfinity XG1300L gyroscope)

http://www.robotnav.com/i2c-interface/
Last edited by lvoc on 20 Nov 2013, 15:12, edited 3 times in total.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post by HaWe »

awsome that you already have written the driver for the cruizcore microinfinity IMU sensor - I'm just about to purchase one!

Do you think your driver lib will work with John's C-API by just #including it ?

Would you mind posting the source code of (all) the drivers one would need for it also here in this forum as a source code? (It's more convenient to read and access them from here instead from your blog)
lvoc
Posts: 38
Joined: 10 Sep 2013, 13:34

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

Post by lvoc »

I think most of this code can be inserted in a C-program and should compile without any issue. Don't forget that there are some header files from the EV3 source code that need to be present (see details at: http://www.robotnav.com/ev3/)
Last edited by lvoc on 17 Oct 2013, 19:40, edited 1 time in total.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post by HaWe »

I personally don't understand that code

Code: Select all

#include <fcntl.h>
#include <stdio.h>
#include  <string.h>
#include  <sys/mman.h>
#include  <sys/ioctl.h>
#include “lms2012.h”
//The ports are designated as PORT_NUMBER-1
const char PORT = 0×0;
const int MAX_SAMPLES  = 100;
int main()
{
int file;
IIC     *pIic;
IICDAT IicDat;
int i;
int idx;

//Open the device file
if((file = open(IIC_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)
{
printf(“Failed to open device\n”);
return -1;
}
pIic  =  (IIC*)mmap(0, sizeof(IIC), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, file, 0);
if (pIic == MAP_FAILED)
{
printf(“Map failed\n”);
return -1;
}

//Setup IIC to read 2 packets
IicDat.Port = PORT;
IicDat.Time = 0;
IicDat.Repeat = 0;
IicDat.RdLng = 2;
IicDat.WrLng = 2;
// Set the device I2C address
IicDat.WrData[0] = 0×01;
// Specify the register that will be read (0×42 = angle)
IicDat.WrData[1] = 0×42;
// Setup I2C comunication
ioctl(file,IIC_SETUP,&IicDat);

printf(“Device is ready\n”);
for(i = 0;i<MAX_SAMPLES;i++)
{
//COmpute and show angle, the units are hundreds of degress
printf(“Angke [deg]x100 =  %d\n”, pIic->Raw[PORT][pIic->Actual[PORT]][0]*256+pIic->Raw[PORT][pIic->Actual[PORT]][1]);
sleep(1);
}

// Close the device file
printf(“Clossing device\n”);
close(file);
return 0;
}
... but maybe someone else is able to wrap some convient function calls around it like this:
https://sourceforge.net/apps/phpbb/mind ... 862#p16428

then probably also home programmers like me will be able to call sensor values!
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post by HaWe »

ps
as I observed that you use printf for display out, I'm curious how you made this printf work on the EV3 screen.

Code: Select all

printf(“Angke [deg]x100 =  %d\n”, pIic->Raw[PORT][pIic->Actual[PORT]][0]*256+pIic->Raw[PORT][pIic->Actual[PORT]][1]);
is the EV3 screen designed as a char device like a file or a stream in your program?
lvoc
Posts: 38
Joined: 10 Sep 2013, 13:34

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

Post by lvoc »

This code does not use the EV3 display, the program runs in the EV3, but all the interactions occur using a terminal window (in your computer), so the program output will be displayed in your computer terminal. For now, I have been trying to get examples for the most important parts of the EV3, I agree that it would be nice to have wrapping functions that hide some of the complexity. Perhaps a good simple approach of doing this is the one posted by "Totokan" in the following thread:
https://sourceforge.net/apps/phpbb/mind ... =10#p17211
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post by HaWe »

ah - a pity, again another terminal user^^ - but I don't use terminals (and probably won't do it, I didn't even use a Linux command ever) - I hoped you were the 1st one who got finally the solution for the EV3 screen char device thing. :(
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post by HaWe »

would it be possible to make a wrap around this XG-1300 sensor function like

ReadSensor(int port, SENSOR_XG1300, int & values[5])

[gyrorotspeedX, gyroangleX, accelX, accelY, accelZ]

?
lvoc
Posts: 38
Joined: 10 Sep 2013, 13:34

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

Post by lvoc »

I wrote an example that shows how to convert the program the handles the XG1300L gyro in functional code. There are several ways of doing this, and this example shows one way that can be applied to other examples.
http://www.robotnav.com/encapsulating-code/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post by HaWe »

thank you for sharing your code!
I immediately tried it out but the compiler/linker cant' find lms2012.h
Can you help me with this?
Are there still other files needed?

ps:
in your calling function

Code: Select all

void get_xg1300l_gyro(float *angle, float *rate, float *acc_x, float *acc_y, float *acc_z)
I don't see a parameter to pass the sensor port number where the XG1300L is attached to. How can one do it?

I mean sth like

Code: Select all

void get_xg1300l_gyro(char port, float *angle, float *rate, float *acc_x, float *acc_y, float *acc_z) 
pps:
I observed that you defined a "constant" global variable

Code: Select all

const char XGL_PORT = 0x0;
maybe it could be passed as a normal variable (char) for the related port to the init, get , and close function.

What are the actual port numbers like? 0, 1, 2, 3 ?
Last edited by HaWe on 05 Nov 2013, 14:40, edited 3 times in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest