Analog Input

Lego uses an ANALOG structure to map the low level device information into user space. The only analog sensor available with the Lego Ev3 is the touch sensor, however there are several NXT sensors that use this type of interface. When the Ev3 reads the data from this sensors, there is a difference in the way it makes it available to the user. Ev3 sensor results (touch sensor) are available in the Pin6 array, while the NXT sensor results (have only tested light sensor) are available in Pin1. In the examples, this difference is shown in Line 35.
The following examples show how to read data from the Ev3 Touch Sensor and from the NXT Light Sensor


Ev3 Touch Sensor or ADC

This code can also be used to use the port as an analog to digital converter, I tested it with the hardware showed in this tutorial.

/*
* Robot Navigation Program
* www.robotnav.com
*
* (C) Copyright 2013 - 2014 Navigation Solutions, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include
#include
#include <sys/mman.h>
#include "lms2012.h"

const char PORT = 0x0; //EV3 label minus 1
const int MAX_SAMPLES = 100;

int main()
{
//DEFINE VARIABLES
int file;
ANALOG *pAnalog;
int i;

//INITIALIZE DEVICE
//Open the device file
if((file = open(ANALOG_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)
{
printf("Failed to open device\n");
return -1;
}
pAnalog = (ANALOG*)mmap(0, sizeof(ANALOG), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, file, 0);
if (pAnalog == MAP_FAILED)
{
printf("Mapping device failed\n");
return -1;
}
printf("Device ready\n");

//PROCESS SENSOR DATA
//Read samples
for(i = 0;i<MAX_SAMPLES;i++) { printf("Analog Value = %d \n",(unsigned int)pAnalog->Pin6[PORT][pAnalog->Actual[PORT]]);
sleep(1);
}

//CLOSE DEVICE
printf("Closing device\n");
close(file);
return 0;
}


NXT Light Sensor

#include
#include
#include <sys/mman.h>
#include "lms2012.h"

const char PORT = 0x0; //EV3 label minus 1
const int MAX_SAMPLES = 100;

int main()
{
//DEFINE VARIABLES
int file;
ANALOG *pNxtLightSensor;
int i;

//INITIALIZE DEVICE
//Open the device file
if((file = open(ANALOG_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)
{
printf("Failed to open device\n");
return -1;
}
pNxtLightSensor = (ANALOG*)mmap(0, sizeof(ANALOG), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, file, 0);
if (pNxtLightSensor == MAP_FAILED)
{
printf("Failed to map device\n");
return -1;
}
printf("Device ready\n");

//PROCESS SENSOR DATA
//Read samples
for(i = 0;i<MAX_SAMPLES;i++) { printf("Analog Value = %d \n",(unsigned char)pNxtLightSensor->Pin1[PORT][pNxtLightSensor->Actual[PORT]]);
sleep(1);
}

//CLOSE DEVICE
printf("Closing device\n");
close(file);
return 0;
}