Color Sensor

By default the the Ev3 Color Sensor works in reflect mode (see type 29 in Device Types table), in order to set it in color mode, it is necessary to configure it. We can follow a procedure similar to the one used for the IR sensor, to set the sensor in a color mode (2).

/*
* Robot Navigation Program
* www.robotnav.com
*
* (C) Copyright 2013 Lauro Ojeda
*
* 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 <sys/ioctl.h>
#include "lms2012.h"

//Color Sensor constants
const char XGL_PORT = 0x0; //EV3 label minus 1
//More information about modes can be found at http://python-ev3.org/types.html
const char COL_COLOR = 2; // Use Color sensor in color mode
const char COLOR_SENSOR_TYPE = 29;
//Other constanst
const int MAX_SAMPLES = 100;

int main()
{
//DEFINE VARIABLES
int file;
UART *pColorSensor; //Color sensor ser level handler
DEVCON DevCon; // Configuration parameters
int i;

//INITIALIZE DEVICE
//Open the device file
if((file = open(UART_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)
{
printf("Failed to open Color Sensor device\n");
return -1;
}
//Map kernel device to be used at the user space level
pColorSensor = (UART*)mmap(0, sizeof(UART), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, file, 0);
if (pColorSensor == MAP_FAILED)
{
printf("Failed to map Color Sensor device\n");
return -1;
}

//SPECIAL CONFIGURATION
//Set Color Sensor as remote control
DevCon.Mode[PORT] = COL_COLOR;
DevCon.Connection[PORT] = CONN_INPUT_UART;
DevCon.Type[PORT] = COLOR_SENSOR_TYPE; //This instruction has no effect in the code
ioctl(file, UART_SET_CONN, &DevCon);
printf("Device is ready \n");

//PROCESS SENSOR DATA
for(i = 0; i<MAX_SAMPLES; i++)
{
printf("Color Sensor code: %d\n", (unsigned char)pColorSensor->Raw[PORT][pColorSensor->Actual[PORT]][0]);
sleep(1);
}

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