Page 1 of 6
EV3 C-code for third party devices (I2C)
Posted: 07 Oct 2013, 22:28
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/
Re: EV3 C-code for third party devices (I2C)
Posted: 10 Oct 2013, 08:43
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)
Re: EV3 C-code for third party devices (I2C)
Posted: 10 Oct 2013, 15:05
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/)
Re: EV3 C-code for third party devices (I2C)
Posted: 10 Oct 2013, 21:21
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!
Re: EV3 C-code for third party devices (I2C)
Posted: 11 Oct 2013, 06:40
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?
Re: EV3 C-code for third party devices (I2C)
Posted: 11 Oct 2013, 12:26
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
Re: EV3 C-code for third party devices (I2C)
Posted: 11 Oct 2013, 12:38
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. :(
Re: EV3 C-code for third party devices (I2C)
Posted: 31 Oct 2013, 17:27
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]
?
Re: EV3 C-code for third party devices (I2C)
Posted: 05 Nov 2013, 04:44
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/
Re: EV3 C-code for third party devices (I2C)
Posted: 05 Nov 2013, 07:34
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
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 ?