Hello-World

TracNav(TOC)
Hello World is a simple application that does the following:
  • Setup serial port and show how to read and write data
  • Setup the FAT filesystem and show how to write to the MMC slot
  • Control LEDs
  • Use timer
  • Various ways to track the brightest red spot on the image

Sample code for setting up the serial port:

1 // configure uarts
2  cc3_uart_init (0, CC3_UART_RATE_115200, CC3_UART_MODE_8N1,
3                 CC3_UART_BINMODE_TEXT);
4  // Make it so that stdout and stdin are not buffered
5  val = setvbuf (stdout, NULL, _IONBF, 0);
6  val = setvbuf (stdin, NULL, _IONBF, 0);

Set up the camera to use the RGB color space in low resolution with white balance and auto exposure turned on:

1  cc3_camera_init ();
2  cc3_camera_set_colorspace (CC3_COLORSPACE_RGB);
3  cc3_camera_set_resolution (CC3_CAMERA_RESOLUTION_LOW);
4  cc3_camera_set_auto_white_balance (true);
5  cc3_camera_set_auto_exposure (true);

This first method for tracking the brightest red color packs data into the cc3_pixel_t structure.
This allows use of the cc3_get_pixel() function which simplifies accessing data.

 1    // This tells the camera to grab a new frame into the fifo and reset
 2    // any internal location information.
 3    cc3_pixbuf_frame_set_coi(CC3_CHANNEL_ALL);
 4    cc3_pixbuf_load ();
 5
 6    // red search!
 7    // *** slow method for red search
 8    start_time = cc3_timer_get_current_ms();
 9    max_red = 0;
10    my_x = 0;
11    my_y = 0;
12    y = 0;
13    while (cc3_pixbuf_read_rows (img.pix, 1)) {
14      // read a row into the image picture memory from the camera
15      for (uint16_t x = 0; x < img.width; x++) {
16        // get a pixel from the img row memory
17        cc3_get_pixel (&img, x, 0, &my_pix);
18        if (my_pix.channel[CC3_CHANNEL_RED] > max_red) {
19          max_red = my_pix.channel[CC3_CHANNEL_RED];
20          my_x = x;
21          my_y = y;
22        }
23      }
24      y++;
25    }
26    end_time = cc3_timer_get_current_ms();
27
28    printf ("Found max red value %d at %d, %d\n", max_red, my_x, my_y);
29    printf (" cc3_get_pixel version took %d ms to complete\n",
30        end_time - start_time);

The next code example shows how you could access the pixel data directly in memory without using
the cc3_get_pixel() function. This is slightly faster, but now the developer has to keep track of
which color channel is being accessed.

 1    // *** faster method for red search
 2    cc3_pixbuf_rewind();  // use exactly the same pixbuf contents
 3    start_time = cc3_timer_get_current_ms();
 4    max_red = 0;
 5    my_x = 0;
 6    my_y = 0;
 7    y = 0;
 8    while (cc3_pixbuf_read_rows (img.pix, 1)) {
 9      // read a row into the image picture memory from the camera
10      for (uint16_t x = 0; x < img.width * 3; x+=3) {
11    uint8_t red = ((uint8_t *) img.pix)[x + CC3_CHANNEL_RED];
12        if (red > max_red) {
13          max_red = red;
14          my_x = x;
15          my_y = y;
16        }
17      }
18      y++;
19    }
20    my_x /= 3; // correct channel offset
21    end_time = cc3_timer_get_current_ms();
22
23    printf ("Found max red value %d at %d, %d\n", max_red, my_x, my_y);
24    printf (" faster version took %d ms to complete\n",
25        end_time - start_time);
26

This final example is the fastest because it grabs only a single channel of the image.
The CMUcam can now quickly clock out unused pixels without reading them into memory.
The data is still stored in the FIFO, so it would be possible to rewind the frame, change the channel
and still access the other color channels. This currently cannot be done mid-image.

 1   // *** even faster method for red search
 2    cc3_pixbuf_rewind();  // use exactly the same pixbuf contents
 3    cc3_pixbuf_frame_set_coi(CC3_CHANNEL_RED);
 4    start_time = cc3_timer_get_current_ms();
 5    max_red = 0;
 6    my_x = 0;
 7    my_y = 0;
 8    y = 0;
 9    while (cc3_pixbuf_read_rows (img.pix, 1)) {
10      // read a row into the image picture memory from the camera
11      for (uint16_t x = 0; x < img.width; x++) {
12    uint8_t red = ((uint8_t *) img.pix)[x];
13        if (red > max_red) {
14          max_red = red;
15          my_x = x;
16          my_y = y;
17        }
18      }
19      y++;
20    }
21    end_time = cc3_timer_get_current_ms();
22
23    printf ("Found max red value %d at %d, %d\n", max_red, my_x, my_y);
24    printf (" even faster version took %d ms to complete\n",
25        end_time - start_time);
26