Skip to content
Snippets Groups Projects
Commit de1a8c0e authored by Václav Jelínek's avatar Václav Jelínek
Browse files

Change number of measurement cycles and color recognition for OC rgb sensor

parent 78d92866
No related branches found
No related tags found
No related merge requests found
......@@ -4,13 +4,26 @@
#ifndef OC_SENSOR_MODES_H
#define OC_SENSOR_MODES_H
#include <stdint.h>
#define SENSOR_ID 193
#define DATA_BAUD_RATE 256000
#define PCT_MIN 0
#define PCT_MAX 100
#define ADC_MAX 65535
#define RAW_MIN 0
#define RAW_MAX 1023
#define MEAS_CYCLES 12
#define MEAS_CYCLES_RGB 6
#define MEAS_CYCLES_AMBIENT 15
#define COL_THS 0.1
#define MEAS_CYCLES 1
typedef struct {
volatile uint8_t data_ready;
volatile uint8_t send_data;
volatile uint8_t change_mode;
} oc_rgb_sensor;
typedef enum {
S_MODE_WAIT_ACK = -2,
......
......@@ -30,11 +30,6 @@
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef struct {
volatile uint8_t data_ready;
volatile uint8_t send_data;
volatile uint8_t change_mode;
} oc_rgb_sensor;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
......@@ -95,7 +90,7 @@ const uint16_t g_max = 24000;
const uint16_t b_min = 0;
const uint16_t b_max = 53000;
uint16_t color=0;
uint8_t color=0;
/* USER CODE END PV */
......@@ -146,10 +141,7 @@ void reset_communication() {
uint16_t meas_light(void){
HAL_ADC_Start(&hadc1);
uint32_t tim1 = __HAL_TIM_GET_COUNTER(&htim1);
HAL_ADC_PollForConversion(&hadc1,1000);
uint32_t tim2 = __HAL_TIM_GET_COUNTER(&htim1);
uint32_t tim3 = tim2-tim1;
return (uint16_t)HAL_ADC_GetValue(&hadc1);
}
......@@ -267,7 +259,7 @@ void rgb_to_hsv(float r, float g, float b, float* h, float* s, float* v) {
uint8_t hsv_to_color(float h, float s, float v) {
if (v < 15) {
return BLACK;
} else if (s < 15) {
} else if (s < 20) {
return WHITE;
} else if (h > 160 && h <= 260) {
return BLUE;
......@@ -294,19 +286,19 @@ float MAP(uint16_t au16_IN, uint16_t au16_INmin, uint16_t au16_INmax, uint16_t a
}
void measure_data(void) {
blue_off();
green_off();
red_off();
switch (sensor_state) {
case S_MODE_REFLECT:
case S_MODE_REF_RAW:
adc_off_sum = 0;
red_sum = 0;
for (int i=0; i < MEAS_CYCLES; i++) {
blue_off();
green_off();
red_off();
delay_us(200);
delay_us(50);
adc_off_sum += meas_light();
red_on();
delay_us(400);
delay_us(50);
red_sum += meas_light();
red_off();
}
......@@ -325,13 +317,10 @@ void measure_data(void) {
adc_off_sum = 0;
green_sum = 0;
for (int i=0; i < MEAS_CYCLES; i++) {
blue_off();
green_off();
red_off();
delay_us(200);
delay_us(50);
adc_off_sum += meas_light();
green_on();
delay_us(400);
delay_us(50);
green_sum += meas_light();
green_off();
}
......@@ -342,13 +331,10 @@ void measure_data(void) {
adc_off_sum = 0;
blue_sum = 0;
for (int i=0; i < MEAS_CYCLES; i++) {
blue_off();
green_off();
red_off();
delay_us(200);
delay_us(50);
adc_off_sum += meas_light();
blue_on();
delay_us(400);
delay_us(50);
blue_sum += meas_light();
blue_off();
}
......@@ -356,65 +342,55 @@ void measure_data(void) {
blue = blue_sum/MEAS_CYCLES;
break;
case S_MODE_AMBIENT:
blue_off();
green_off();
red_off();
adc_off_sum = 0;
for (int i=0; i < MEAS_CYCLES; i++) {
for (int i=0; i < MEAS_CYCLES_AMBIENT; i++) {
adc_off_sum += meas_light();
delay_us(100);
delay_us(10);
}
adc_off = adc_off_sum/MEAS_CYCLES;
adc_off = adc_off_sum/MEAS_CYCLES_AMBIENT;
ambient = MAP(adc_off, 0, ADC_MAX, PCT_MIN, PCT_MAX);
break;
case S_MODE_COLOR:
case S_MODE_RGB_RAW:
blue_off();
green_off();
red_off();
adc_off_sum = 0;
red_sum = 0;
green_sum = 0;
blue_sum = 0;
for (int i=0; i < MEAS_CYCLES; i++) {
delay_us(200);
for (int i=0; i < MEAS_CYCLES_RGB; i++) {
delay_us(50);
adc_off_sum += meas_light();
red_on();
delay_us(400);
delay_us(50);
red_sum += meas_light();
red_off();
green_on();
delay_us(400);
delay_us(50);
green_sum += meas_light();
green_off();
blue_on();
delay_us(400);
delay_us(50);
blue_sum += meas_light();
blue_off();
}
if (red_sum < adc_off_sum) {
red_diff = 0;
} else {
red_diff = (red_sum - adc_off_sum)/MEAS_CYCLES;
}
if (green_sum < adc_off_sum) {
green_diff = 0;
} else {
green_diff = (green_sum - adc_off_sum)/MEAS_CYCLES;
}
if (blue_sum < adc_off_sum) {
blue_diff = 0;
} else {
blue_diff = (blue_sum - adc_off_sum)/MEAS_CYCLES;
}
adc_off = adc_off_sum/MEAS_CYCLES;
red_on();
green_on();
blue_on();
red_diff = red_sum / MEAS_CYCLES_RGB;
green_diff = green_sum / MEAS_CYCLES_RGB;
blue_diff = blue_sum / MEAS_CYCLES_RGB;
adc_off = adc_off_sum / MEAS_CYCLES_RGB;
red_map = MAP(red_diff, r_min, r_max, 0, 1);
green_map = MAP(green_diff, g_min, g_max, 0, 1);
blue_map = MAP(blue_diff, b_min, b_max, 0, 1);
rgb_to_hsv(red_map, green_map, blue_map, &h, &s, &v);
if (red_map < COL_THS && green_map < COL_THS && blue_map < COL_THS) {
color = NONE;
} else {
color = hsv_to_color(h,s,v);
}
break;
default:
break;
......@@ -452,7 +428,6 @@ void choose_send_data(void) {
send_data(sensor_state, &ambient, sizeof(ambient), USART1);
break;
case S_MODE_COLOR:
color = hsv_to_color(h,s,v);
send_data(sensor_state, &color, sizeof(color), USART1);
break;
case S_MODE_RGB_RAW: {
......
......@@ -29,11 +29,11 @@ const ev3_mode_info m_ambient = {S_MODE_AMBIENT,
const ev3_mode_info m_color = {S_MODE_COLOR,
"COL-COLOR", 9,
0, 7,
0, 8,
0, 0,
0, 0,
"col", 3,
1, 1, 1, 0};
1, 0, 1, 0};
const ev3_mode_info m_reflect_raw = {S_MODE_REF_RAW,
"REF-RAW", 7,
......
......@@ -40,7 +40,7 @@
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.stlink_txt_serial_number" value=""/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.watchdog_config" value="none"/>
<booleanAttribute key="com.st.stm32cube.ide.mcu.debug.stlinkenable_rtos" value="false"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlinkrestart_configurations" value="{&quot;fVersion&quot;:1,&quot;fItems&quot;:[{&quot;fDisplayName&quot;:&quot;Reset&quot;,&quot;fIsSuppressible&quot;:false,&quot;fResetAttribute&quot;:&quot;Software system reset&quot;,&quot;fResetStrategies&quot;:[{&quot;fDisplayName&quot;:&quot;Software system reset&quot;,&quot;fLaunchAttribute&quot;:&quot;system_reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset\r\n&quot;],&quot;fCmdOptions&quot;:[&quot;-g&quot;]},{&quot;fDisplayName&quot;:&quot;Hardware reset&quot;,&quot;fLaunchAttribute&quot;:&quot;hardware_reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset hardware\r\n&quot;],&quot;fCmdOptions&quot;:[&quot;-g&quot;]},{&quot;fDisplayName&quot;:&quot;Core reset&quot;,&quot;fLaunchAttribute&quot;:&quot;core_reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset core\r\n&quot;],&quot;fCmdOptions&quot;:[&quot;-g&quot;]},{&quot;fDisplayName&quot;:&quot;None&quot;,&quot;fLaunchAttribute&quot;:&quot;no_reset&quot;,&quot;fGdbCommands&quot;:[],&quot;fCmdOptions&quot;:[&quot;-g&quot;]}],&quot;fGdbCommandGroup&quot;:{&quot;name&quot;:&quot;Additional commands&quot;,&quot;commands&quot;:[]},&quot;fStartApplication&quot;:true}]}"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlinkrestart_configurations" value="{&quot;fVersion&quot;:1,&quot;fItems&quot;:[{&quot;fDisplayName&quot;:&quot;Reset&quot;,&quot;fIsSuppressible&quot;:false,&quot;fResetAttribute&quot;:&quot;Software system reset&quot;,&quot;fResetStrategies&quot;:[{&quot;fDisplayName&quot;:&quot;Software system reset&quot;,&quot;fLaunchAttribute&quot;:&quot;system_reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset\n&quot;],&quot;fCmdOptions&quot;:[&quot;-g&quot;]},{&quot;fDisplayName&quot;:&quot;Hardware reset&quot;,&quot;fLaunchAttribute&quot;:&quot;hardware_reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset hardware\n&quot;],&quot;fCmdOptions&quot;:[&quot;-g&quot;]},{&quot;fDisplayName&quot;:&quot;Core reset&quot;,&quot;fLaunchAttribute&quot;:&quot;core_reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset core\n&quot;],&quot;fCmdOptions&quot;:[&quot;-g&quot;]},{&quot;fDisplayName&quot;:&quot;None&quot;,&quot;fLaunchAttribute&quot;:&quot;no_reset&quot;,&quot;fGdbCommands&quot;:[],&quot;fCmdOptions&quot;:[&quot;-g&quot;]}],&quot;fGdbCommandGroup&quot;:{&quot;name&quot;:&quot;Additional commands&quot;,&quot;commands&quot;:[]},&quot;fStartApplication&quot;:true}]}"/>
<booleanAttribute key="com.st.stm32cube.ide.mcu.rtosproxy.enableRtosProxy" value="false"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.rtosproxy.rtosProxyCustomProperties" value=""/>
<stringAttribute key="com.st.stm32cube.ide.mcu.rtosproxy.rtosProxyDriver" value="threadx"/>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment