From 812a729a20dd9e6af912bb486f8760e05c4d124b Mon Sep 17 00:00:00 2001 From: Zacharyprime Date: Fri, 16 Sep 2022 14:16:50 -0500 Subject: [PATCH 1/2] Adding back graphical wait_for_light --- CMakeLists.txt | 2 +- module/botball/src/botball_c.cpp | 198 ++++++++++++++++--------------- 2 files changed, 102 insertions(+), 98 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f7cde199..93778422 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,4 +108,4 @@ set(CPACK_DEBIAN_PACKAGE_MAINTAINER "KISS Institute for Practical Robotics") set(CPACK_PACKAGE_VERSION_MAJOR ${KIPR_VERSION_MAJOR}) set(CPACK_PACKAGE_VERSION_MINOR ${KIPR_VERSION_MINOR}) set(CPACK_PACKAGE_VERSION_PATCH ${KIPR_VERSION_PATCH}) -include(CPack) \ No newline at end of file +include(CPack) diff --git a/module/botball/src/botball_c.cpp b/module/botball/src/botball_c.cpp index 7d992b0d..050fbe0b 100644 --- a/module/botball/src/botball_c.cpp +++ b/module/botball/src/botball_c.cpp @@ -3,6 +3,10 @@ #include "kipr/button/button.h" #include "kipr/analog/analog.h" #include "kipr/console/console.h" +#include "kipr/graphics/graphics.h" +#include "kipr/graphics/graphics_characters.h" +#include "kipr/console/display.h" +#include "kipr/console/console.h" #include #include @@ -59,105 +63,105 @@ void shut_down_in(double s) } -//Waits for the light to shine on the light sensor. -//Ignores camera flashes -//Zachary Sasser - zsasser@kipr.org +// Rework of Wallaby version of wait_for_light to work with the Wombat KRC +// cnw: 8/15/2019 +#define FLASHPAUSE 100 // msecs to pause to let a flash subside +/* Assumption is that if light is detected by consecutive readings FLASHPAUSE apart, + it's the start light, not a flash + NOTE: when tested with camera, the flash persisted < 0.1 sec +*/ +#define THRESHOLD 60 // minimum discrimination between light and dark +#define RED 255,0,0 +#define GREEN 0,255,0 +#define WHITE 255,255,255 +#define BLACK 0,0,0 +#define CROWS 200 // Cylon window dimensions +#define CCOLS 500 +#define INSET 100 // how much to inset Cylon eye +#define DJUMP 2 // how far to jump to next red circle - affects speed of red circle +void colorbar(int i, int range); -void wait_for_light(int port) +//Waits for the light to shine on the light sensor. +void wait_for_light(int light_port) { - int badDifference = 100; //The standard for what is too close for the light values - - while(1>0){ - - while(any_button()){} //Debounce - - while(!any_button()){ - - printf("Turn on light and press button... \n"); - printf("Light ON Value: %d <---- \n", analog(port)); - printf(" |=| \n"); - printf(" / \\ \n"); - printf("--- ( ) --- \n"); - printf(" , `-'. \n"); - printf(" / | \\ \n"); - printf(" ' | ` \n"); - - if(any_button()){ break; } //Makes the button more responsive - - msleep(200); - console_clear(); //For refreshing the values - - } - - int initial = analog(port); //Set Light ON Value - - while(any_button()){} //Debounce - - while(!any_button()){ - printf("Turn off light and press button... \n \n"); - printf("Light ON Value: %d \n", initial); - printf("Light OFF Value: %d <---- \n", analog(port)); - printf(" |=| \n"); - printf(" / \\ \n"); - printf(" ( ) \n"); - printf(" `-' \n"); - - if(any_button()){ break; } //Makes the button more responsive - - msleep(200); - console_clear(); //For refreshing the values - - } - - int final = analog(port); //Set the light off value - - while(any_button()){} //Debounce - - if((final-initial)>badDifference){ //Check to make sure the values are good. - - //If the light is 10% of the way to the ON value, it is a light. - int threshold = (final-initial)*0.1+initial; - - /* - This part keeps camera flashes from starting the robot. The 2 second wait is how long my cell phone flash takes. -Zach - */ - while(analog(port)>threshold){ - while(analog(port)>threshold){ - printf("Waiting for starting light...\n \n"); - printf("Light ON Value: %d \n", initial); - printf("Light OFF Value: %d \n", final); - printf("---------------------- \n"); - printf("Threshold Value: %d \n \n", threshold); - printf("Current Value: %d <---- \n", analog(port)); - msleep(200); - console_clear(); - + int OK=0, onval, offval, reading, i=0, range; + while (!OK) { + display_printf(0,0,"CALIBRATE: sensor port #%d", light_port); + display_printf(0,1," press 'B' button when light is on"); + while(b_button()==0) { + msleep(100); + onval=analog(light_port); // sensor value when light is on + display_printf(0,3," light on value is = %4d ", onval); + } + display_printf(0,3," light on value is = %4d ", onval); + msleep(200); + while (b_button()); // debounce B button + display_printf(0,1," press 'B' button when light is off"); + display_printf(0,2," (OFF must exceed ON by at least %d)\n", THRESHOLD); + while (b_button()==0) { + offval=analog(light_port); + display_printf(0,4," light off value is = %4d ", offval); + display_printf(0,5," ---- "); + display_printf(0,6," OFF-ON = %4d ",offval-onval); + msleep(100); + } + offval=analog(light_port); // sensor value when light is off + display_printf(0,4," light off value is = %4d ", offval); + display_printf(0,6," OFF-ON = %4d ", offval-onval); + msleep(200); + if ((offval-onval)>=THRESHOLD) { // bright = small values + OK=1; + display_printf(0,8,"Good Calibration!"); + msleep(1000); + graphics_open(CCOLS,CROWS); + graphics_fill(WHITE); // paint screen + range=CCOLS-2*INSET; // inset amount for green bar that follows + graphics_rectangle_fill(INSET,CROWS/2-20,CCOLS-INSET,CROWS/2+20,GREEN); + graphics_circle_fill(INSET,CROWS/2,20,GREEN); + graphics_circle_fill(CCOLS-INSET,CROWS/2,20,GREEN); + graphics_update(); + graphics_print_string("READING - ON = ",INSET,30,BLACK,2); + while (1) { // loop until light verified on + colorbar(i++, range); // draw moving red circle + //msleep(20); // alternate for setting red circle speed to DJUMP + reading=analog(light_port); + graphics_rectangle_fill(INSET+210,30,INSET+270,60,WHITE); // erase text + graphics_print_int(reading-onval,1,INSET+210,30,BLACK,2); + if ((reading-onval) < THRESHOLD) { // reading is low enough for light on + msleep(FLASHPAUSE); // pause + reading=analog(light_port); // get second reading to rule out flash + if ((reading-onval) < THRESHOLD) break; // if still low enough, light is on + } + } + graphics_close(); + console_clear(); + printf("Diagnostics:\n"); + printf("OFF: %d, ON: %d, OFF-ON: %d\n", offval, onval, offval-onval); + printf("READ: %d, READ-ON=%d < THRESHOLD=%d\n",reading, reading-onval, THRESHOLD); + printf(" (good enough to recognize lights on)\n"); + } + else { + display_printf(0,8,"BAD CALIBRATION"); + if (offval<256) { + display_printf(15,8," - Add Shielding!!"); + msleep(5000); + } + else { + display_printf(15,8," - Aim sensor!!"); + msleep(5000); + } + display_clear(); } - printf("Waiting for starting light...\n \n"); - printf("Light ON Value: %d \n", initial); - printf("Light OFF Value: %d \n", final); - printf("---------------------- \n"); - printf("Threshold Value: %d \n \n", threshold); - printf("Current Value: %d <---- \n", analog(port)); - msleep(2000); - console_clear(); - } - return; } +} - //If the values were too close together - else{ - - - console_clear(); - printf("BAD CALIBRATION!!!"); - if(final<1000){ - printf(" -> Shield Light Sensor. \n"); - } - else{ - printf(" -> Values are too close.\n"); - } - printf(" \n \n Press any button to restart calibration."); - while(!any_button()){} //Wait until they press the button. - } - }} +void colorbar(int i, int range) { + int d, limit; + limit = range/DJUMP; // how far to traverse before reversing + d=i%(int)(2*limit); // scale i to total number of jumps (half forward and half back) + if (d Date: Fri, 16 Sep 2022 16:55:47 -0500 Subject: [PATCH 2/2] Added graphics dependency to CMakeLists of botball module --- module/botball/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/module/botball/CMakeLists.txt b/module/botball/CMakeLists.txt index b04efa08..ae9baba7 100644 --- a/module/botball/CMakeLists.txt +++ b/module/botball/CMakeLists.txt @@ -1,5 +1,5 @@ option(with_botball "Enable botball API" ON) if(with_botball) - add_module(botball core thread analog button console time) -endif() \ No newline at end of file + add_module(botball core thread analog button console time graphics) +endif()