Rose-Hulman Robotics Team

Changeset 490

Show
Ignore:
Timestamp:
05/14/09 01:04:25 (3 years ago)
Author:
spenceal
Message:

obsticle avoidance code

Location:
trunk/software/rb
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • trunk/software/rb/auto.py

    r488 r490  
    2020import time 
    2121from math import pi 
     22from collections import defaultdict 
    2223 
    2324from rb.core.logging import log_debug, log_info, log_warn, log_error, log_die 
    2425from rb.utils import points_to_vec, rad_delta 
    25  
    2626 
    2727from time import sleep 
     
    4444        def __init__(self, controller): 
    4545                self.controller = controller 
     46                self.running = False 
     47                self.points = [] 
    4648 
    4749                # self.nav_func = navigate_points 
    48                 self.nav_func = navigate_course 
     50                self.nav_func = self.navigate_course 
    4951         
    5052        def _main(self): 
    51                 if self.controller.navigator == 'auto': 
    52                         self.nav_func() 
    53                         # update motors  
     53                print "Auto._main: starting main loop" 
     54                self.running = True 
     55                while (self.running): 
     56                        if self.controller.navigator == 'auto': 
     57                                print "Auto._main: Running autonomous code" 
     58                                self.nav_func() 
     59                                # update motors  
     60                        sleep(0.5) 
     61                print "Auto._main: Finished" 
    5462 
    5563        def process(self, type, data): 
    5664                if type == 'vision_points': 
     65                        print "Auto.process: Storing points" 
    5766                        self.points = data[0] 
    5867 
    5968        def navigate_course(self): 
    60                 for point in self.points(): 
    61                         # count points `in front' of us 
    62                         # If it's `mostly' clear 
    63                         #       go forward 
    64                         # else 
    65                         #       count points on left/right 
    66                         #       turn in most clear direction 
     69                # TODO: import these from the C enum 
     70                RB_GRASS       = 0 
     71                RB_OBSTICLE    = 1 
     72                RB_LINE        = 2 
     73                RB_IGNORE      = 3 
     74                values = [0.1, 0.1, 0.1, 0.1] 
     75 
     76                # count points `in front' of us 
     77                for point in self.points: 
     78                        if (-10 < point[0] and point[0] < 10 and point[0] < 10): 
     79                                values[point[2]] += 1 
     80 
     81                if (values[RB_GRASS] < 10): 
     82                        print "Auto.navigate_course: Not enough data, aborting"  
     83                        return False; 
     84 
     85                # If it's `mostly' clear 
     86                print "Auto.navigate_course: values = ", values  
     87                if (values[RB_GRASS]/values[RB_OBSTICLE]) > 2: 
     88                        # go forward 
     89                        print "Auto.navigate_course: Moving forward"  
     90                        self.controller.drive.setspeed(0.5, 0.5) 
     91                else: 
     92                        print "Auto.navigate_course: Obsticle detected"  
     93                        # TODO: 
     94                        # count points on left/right 
     95                        # turn in most clear direction 
    6796 
    6897        def navigate_points(self): 
  • trunk/software/rb/controller.py

    r488 r490  
    6969                from rb.auto import Auto 
    7070                self.auto = Auto(self) 
     71                self.auto._main() 
    7172 
    7273        def _run_wiimote(self): 
     
    99100                                return False 
    100101                elif nav == 'auto': 
    101                         print "Not implemented" 
    102                         return False 
     102                        pass 
    103103                elif nav == 'stop': 
    104104                        self.drive.setspeed(0, 0) 
  • trunk/software/rb/vision/__init__.py

    r489 r490  
    99                self.controller = controller 
    1010                _vision._init_classifier() 
    11                 _vision._init_capture() 
     11                #_vision._init_capture() 
    1212                start_new_thread(self._main, ()) 
    1313 
  • trunk/software/rb/vision/_vision.c

    r489 r490  
    102102_get_points(PyObject *self, PyObject *args) 
    103103{ 
    104         if (classifier == NULL) { 
    105                 PyErr_SetString(PyExc_RuntimeError, "_init_classifier must be called first"); 
    106                 return NULL; 
    107         } else if (cam == NULL) { 
    108                 PyErr_SetString(PyExc_RuntimeError, "_init_capture must be called first"); 
    109                 return NULL; 
    110         } 
     104        //if (classifier == NULL) { 
     105        //      PyErr_SetString(PyExc_RuntimeError, "_init_classifier must be called first"); 
     106        //      return NULL; 
     107        //} else if (cam == NULL) { 
     108        //      PyErr_SetString(PyExc_RuntimeError, "_init_capture must be called first"); 
     109        //      return NULL; 
     110        //} 
    111111 
    112112        // TODO: start with a larger list 
    113113        printf("generating new pixbuf\n"); 
    114         PyObject *points = PyList_New(0); 
     114        PyObject *py_points = PyList_New(0); 
    115115 
    116116        IplImage *input; 
     
    124124        Py_BEGIN_ALLOW_THREADS 
    125125 
    126         input       = capture(); 
     126        //input       = capture(); 
     127        input       = get_image("rb/vision/data/classes/input.jpeg"); 
    127128        transformed = transform_image(input, 0.25); 
    128129        classes     = classifier_predict(classifier, transformed); 
     
    135136        int i; 
    136137        for(i = 0; i < seq->total; i++) { 
    137                 int data[3]; 
    138                 CV_READ_SEQ_ELEM(data, reader); 
    139                 PyObject *point = Py_BuildValue("iii", 
    140                                 data[0], data[1], data[2]); 
    141                 PyList_Append(points, point); 
     138                point_t point; 
     139                CV_READ_SEQ_ELEM(point, reader); 
     140                PyObject *py_point = Py_BuildValue("iii", 
     141                                point.x, point.y, point.value); 
     142                PyList_Append(py_points, py_point); 
    142143        } 
    143144 
     
    151152        cvReleaseMat(&classes); 
    152153 
    153         PyObject *rval = Py_BuildValue("(OO)", points, py_pixbuf); 
    154         Py_DecRef(points); 
     154        PyObject *rval = Py_BuildValue("(OO)", py_points, py_pixbuf); 
     155        Py_DecRef(py_points); 
    155156        Py_DecRef(py_pixbuf); 
    156157        return rval; 
     
    162163        {"_init_classifier", _init_classifier, METH_VARARGS, 
    163164         "Initialize the classifier using predefined training images"}, 
    164     {"_init_capture", _init_capture, METH_VARARGS, 
    165     "Initialize the camera capture device"}, 
     165        {"_init_capture", _init_capture, METH_VARARGS, 
     166        "Initialize the camera capture device"}, 
    166167        {NULL, NULL, 0, NULL} // sentinel 
    167168}; 
  • trunk/software/rb/vision/classify_dtree.cpp

    r484 r490  
    3232        dtree->train(input, CV_ROW_SAMPLE, desired, 0, 0, var_type, NULL, 
    3333                CvDTreeParams( 
    34                         5,  // 15,             // Max depth 
    35                         10, // 50,             // min sample count 
     34                        15, // 5,       // Max depth 
     35                        50, // 10,      // min sample count 
    3636                        0,              // regression accuracy: N/A here 
    3737                        false,          // no not compute surrogate split 
    3838                        RB_NUM_CLASSES, // max number of categories (use sub-optimal algorithm for larger numbers) 
    39                         4, // 20,             // the number of cross-validation folds 
     39                        20, // 4        // the number of cross-validation folds 
    4040                        false,          // use 1SE rule => smaller tree 
    4141                        true,           // throw away the pruned tree branches 
  • trunk/software/rb/vision/main.c

    r486 r490  
    4242        /* Process */ 
    4343        print_time("Training classifier"); 
    44         classifier_t *classifier = classifier_new(RB_DTREE); 
     44        //classifier_t *classifier = classifier_new(RB_DTREE); 
     45        classifier_t *classifier = classifier_new(RB_HARD); 
    4546        classifier_add_data_from_files(classifier, 
    4647                        "data/classes/input.jpeg", 
     
    5960        cvStartReadSeq(seq, &reader, 0); 
    6061        for(int i = 0; i < seq->total; i++) { 
    61                 int data[3]; 
    62                 CV_READ_SEQ_ELEM(data, reader); 
     62                point_t point; 
     63                CV_READ_SEQ_ELEM(point, reader); 
    6364                cvCircle(transformed, 
    64                          cvPoint(data[1], data[2]), 
    65                          2, class_colors[data[0]], CV_FILLED, 8, 0); 
     65                         cvPoint(point.x + transformed->width/2, 
     66                                 transformed->height - point.y), 
     67                         2, class_colors[point.value], CV_FILLED, 8, 0); 
    6668        } 
    6769        cvReleaseMemStorage(&seq->storage); 
  • trunk/software/rb/vision/makefile

    r488 r490  
    11CFLAGS   = -p -pg -g -Wall -Werror -Wno-unused -pedantic -fPIC --std=gnu99 -fgnu89-inline -O3 $(shell pkg-config --cflags opencv gsl) 
    22#CFLAGS   = -p -pg -g -Wall -Werror -pedantic -fPIC -O3 $(shell pkg-config --cflags opencv gsl) 
    3 CPPFLAGS = -p -pg -g -Wall -Werror --Wno-unused pedantic -fPIC -O3 $(shell pkg-config --cflags opencv gsl) 
     3CPPFLAGS = -p -pg -g -Wall -Werror -Wno-unused -pedantic -fPIC -O3 $(shell pkg-config --cflags opencv gsl) 
    44LDFLAGS  = $(shell pkg-config --libs opencv gsl) 
    55DLFLAGS  = -shared -nostartfiles 
  • trunk/software/rb/vision/vision.c

    r489 r490  
    5050        /* transform image */ 
    5151        IplImage *transformed = cvCreateImage(size, img->depth, img->nChannels); 
    52         CvMat* mat= cvCreateMat( 3, 3, CV_32F ); 
     52        CvMat *mat = cvCreateMat(3, 3, CV_32F); 
    5353        CvPoint2D32f from[] = { 
    54                 cvPoint2D32f(1208, 1172), 
    55                 cvPoint2D32f(1196, 1224), 
    56                 cvPoint2D32f(1295, 1178), 
    57                 cvPoint2D32f(1294, 1231) 
     54                cvPoint2D32f(0           , size.height*0.12), 
     55                cvPoint2D32f(0           , size.height-1), 
     56                cvPoint2D32f(size.width-1, size.height-1), 
     57                cvPoint2D32f(size.width-1, size.height*0.12) 
    5858        }; 
    5959        CvPoint2D32f to[] = { 
    60                 cvPoint2D32f(1210, 1170), 
    61                 cvPoint2D32f(1210, 1250), 
    62                 cvPoint2D32f(1290, 1170), 
    63                 cvPoint2D32f(1290, 1250) 
     60                cvPoint2D32f(0              , -62          ), 
     61                cvPoint2D32f(size.width*0.33, size.height-1), 
     62                cvPoint2D32f(size.width*0.67, size.height-1), 
     63                cvPoint2D32f(size.width-1   , -62          ) 
    6464        }; 
    6565        cvGetPerspectiveTransform(from, to, mat); 
     
    6969} 
    7070 
    71 //int [3]point_to_ 
     71/* convert an x,y,value to a point_t 
     72 * (changes x,y vlaues to be centered on the robot) 
     73 * TODO: Do this using the microstrain */ 
     74point_t xyv_to_point(CvMat *im, int x, int y, CvScalar value) 
     75{ 
     76        point_t point; 
     77        point.x     = x - (im->cols/2); 
     78        point.y     = im->rows - y; 
     79        point.value = value.val[0]; 
     80        return point; 
     81} 
    7282 
    7383CvSeq *get_points(CvMat *classes) 
    7484{ 
    7585        CvMemStorage *storage = cvCreateMemStorage(0); 
    76         CvSeq *seq = cvCreateSeq(CV_32SC3,      // sequence of 3 tuples? (scalars?) 
    77                                  sizeof(CvSeq), // header size - no extra fields 
    78                                  sizeof(int)*3, // element size 
    79                                  storage);      // the container storage 
     86        CvSeq *seq = cvCreateSeq(CV_32SC3,        // sequence of 3 tuples? (scalars?) 
     87                                 sizeof(CvSeq),   // header size - no extra fields 
     88                                 sizeof(point_t), // element size 
     89                                 storage);        // the container storage 
    8090 
    8191        CvSeqWriter writer; 
     
    8494                for(int y = 0; y < classes->rows; y+=10) { 
    8595                        CvScalar klass = cvGet2D(classes, y, x); 
    86                         int data[3] = {klass.val[0], x, y}; 
    87                         CV_WRITE_SEQ_ELEM(data, writer); 
     96                        point_t point = xyv_to_point(classes, x, y, klass); 
     97                        CV_WRITE_SEQ_ELEM(point, writer); 
    8898                } 
    8999        }