Page 4 of 7
Re: Bounty for complete EV3 C/C++ API
Posted: 19 Jan 2014, 17:32
by HaWe
as I currently have 2 of them without any useful avail I can borrow you one of mine - you're living in Holland, don't you?
Are you interested?
Re: Bounty for complete EV3 C/C++ API
Posted: 19 Jan 2014, 17:38
by totokan
While I appreciate the kind offer, I must decline. I am in the US, not sure where you got the idea I was in Holland from, haha!
Re: Bounty for complete EV3 C/C++ API
Posted: 19 Jan 2014, 17:41
by HaWe
sorry, I must have confused your home state with pipijndevos' :)
Re: Bounty for complete EV3 C/C++ API
Posted: 19 Jan 2014, 18:05
by HaWe
ps,
but if you have an idea how to use I²C sensors in very simple NXC-like way?
For the start I had to plug 4 I²C sensors:
1x XG1300 gyro (the same as Lauro is using)
2x NXT (!!) Ultrasonic sensor (possibly in single shot mode)
1x PCF8574 homebrewed touch sensor muxer
(for the PCF thing the NXC code is the following:
Code: Select all
#define BitRead(_Value, _Bit) !(0x01 & (_Value >> _Bit ) ) // 0...7
int PCF8574ReadPins(byte port, byte address){
byte cnt = 2;
byte I2CMsg[];
byte inbuf[];
byte nByteReady = 0;
int result = -1;
ArrayBuild(I2CMsg, address);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING){
Yield();
}
if (I2CBytes(port, I2CMsg, cnt, inbuf)) {
result = inbuf[1];
}
if(result==-1){
TextOut(0, LCD_LINE1, "Error: Check the");
TextOut(0, LCD_LINE2, " connection!");
Wait(500);
ClearScreen();
}
return result;
}
task main() {
byte PCF8574array[8]; // slots 0...7 to access
byte PCF8574port=S4, PCF8574addr=0x4E, result;
SetSensorLowspeed(S4);
while(true) {
result=PCF8574ReadPins(PCF8574port, PCF8574addr); // poll the PCF byte once
for (char i=0; i<8; ++i) {
PCF8574array[i]= BitRead(result, i); // bit read the PCF byte
NumOut( 0, 56-(8*i), i);
NumOut(18, 56-(8*i), PCF8574array[i]);
}
Wait(5);
}
}
If I knew how to access them NXC-like (or RobotC-like) on the EV3 I could finally already start with some navigation robot experiments by my own...
Re: Bounty for complete EV3 C/C++ API
Posted: 23 Jan 2014, 22:32
by ulmeco
I bought the EV3 kit a week ago, and having used Bricxcc/NQC many years ago I figured I'd use the C api.
Since there's no API for sensors yet I decided to try to hack together something that works. Somehow I missed the fact that there's already some support for it available at robotnav.com, with source code as well. There's even a link to it in the first post. Discovering that earlier would have made things easier.
Anyway, after working on it in the evenings, looking at the EV3 firmware source code and Hansen's API, I got what I suppose is the most basic feature working - reading the sensor value (for UART sensors).
I've attached source code for that. My plan now is just to add some more features like changing sensor mode, and probably I²C sensor support (I assume I²C == IIC ?)... and then put it up on github.
Re: Bounty for complete EV3 C/C++ API
Posted: 23 Jan 2014, 22:36
by totokan
I have made some headway into generic sensor reading code. Hopefully, I will be able to complete a simple 'ReadSensor(port)' function that will return whatever information we expect from the sensor at the specified port. A similar function exists in the official LEGO stuff, it is used in the port view application.
As soon as I've finished something that works, I'll put it up here for general consumption. As previously mentioned, we do not currently have any Ev3 bricks for development, so this could take some time.
Re: Bounty for complete EV3 C/C++ API
Posted: 23 Jan 2014, 22:49
by ulmeco
Right, I saw you were working on that in an earlier post. That will probably be more feature complete than whatever I'm doing.
I thought I'd throw my code out there now, as early as possible, in case there would be anyone else that wanted to have something to work with.
Re: Bounty for complete EV3 C/C++ API
Posted: 26 Jan 2014, 00:02
by ulmeco
I've added support for other sensor types (IIC etc.), and for setting sensor modes. Available here:
https://github.com/carstenz/ev3sensor
Hope it's useful to someone!
Re: Bounty for complete EV3 C/C++ API
Posted: 26 Jan 2014, 10:44
by HaWe
thank you for sharing your code!
Did you already write a test code which contains readings of all sensor types in 1 single test code so that one would see which files have to be #included and which have to be linked by the project manager, e.g. something like
S1: ev3/UART: ev3 IR sensor in remote control mode
S2: nxt/ADC: nxt touch sensor in boolean reading (released vs. pressed)
S3: nxt/ADC:nxt color sensor R,G,B readings in 3x raw mode plus detected color
S4: nxt/IIC: XG1300L gyro (like used by Lauro (lvoc) - or - PCF8574 touch muxer, IIC device addr = 0x4E (like I showed by NXC source code) - or - nxt ultrasonic sensor
Re: Bounty for complete EV3 C/C++ API
Posted: 26 Jan 2014, 23:02
by ulmeco
You only have to compile ev3sensor.c (add it in project manager in Bricxcc), and then include ev3sensor.h in your source code. ev3sensor.h contains the API functions, like readSensor(port) and setSensorMode(port, mode).
I've written some simple code for testing in example/sensorTest.c. The API is very basic though - it only returns raw sensor values, so for example no R,G,B readings for the EV3 color sensor - that has to be interpreted by looking at the bytes that readSensorData() returns. This is mostly because I don't know how to map the sensor values to R,G,B yet, but also because I only have the sensors that come with the EV3 kit to test with. I wonder if that information is available somewhere, for example in the Bricxcc source code? But I'm not sure if that source code is available. Admittedly I haven't spent much time looking for it.
The NXT color sensor is a special case: I'm actually missing support for that at the moment, but it's a special sensor that I could try to add support for by looking at EV3 firmware code. I began on it (it's commented out in ev3sensor.c), but gave up since I can't actually test it. I can't test any IIC devices either, but I believe they should work - they should return raw sensor values just like UART devices. When you call readSensor() the API looks up the connection type and returns IIC data if the connection is of that type, and so on.
Some sensor types/modes are pretty straightforward to understand from the raw sensor values, for example my EV3 touch sensor returns 164 when released and around 3360 when pressed. IR sensor in proximity mode returns a value between 0 and 100. I would assume the gyro sensor would return a similar value that is easy to understand.