Recent Changes - Search:

Tracking

Hardware

Software

Mega Plan

Etc.

HowToUseBehaviors

How to use Behaviors

For Flockbot programming, there are two primary modes of interaction. The first is through Sockets, which allow any number of client machines to connect to any number of Flockbots and send commands directly to the robot. This allows the programming to execute off of the client machine. The second mode is to enable the robots to operate autonomously through on-board program execution.

This on-board execution is provided by two different systems which are executed on a dedicated behavior thread on the Flockbot. These systems are Dynamic C libraries and Lua scripts. Both of these methods for programming the Flockbot use the same on-board API (API). Each one is executed on demand, either through keyboard entry on a manual execution, or through the appropriate socket command to start a behavior.

The behaviors live in /home/pi/flockbot/behaviors and then either in the c/ or lua/ subdirectories. Once the scripts are constructed, no additional work is needed to prepare them for execution. Selecting a behavior while the Flockbot is running will cause the system to perform an SVN update to get any changes and then will prepare the libraries for execution.

If any elements of the API appears to be non-functional or missing (Lua hasn't been updated in a while out of lack of immediate interest), please email kandrea.

C

These libraries are compiled by the Flockbot to be dynamic libraries. What this means is that you can load it in at runtime, execute it, then unload it at its completion. This way you can have many different behavior libraries that all have the same function names.

The entry point for a C behavior library is:

void *start_behavior(void *args) {

}

As this is C, you can create your own threads (after adding the appropriate header files), source from other source files, and anything else you can do in C. The API is fully available to you for the robot.

The following is a sample C behavior:

#include <stdio.h>

void *start_behavior(void *args) {
  printf("Staring motion\n");
  move_distance(10, 20);
  wait_for_mc();
  turn_robot_wait(30, 180);
  move_distance_wait(30,20);
  claw_close();
  return;
}

C with Barcode Vision

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <math.h>
#include "barcode.h"
#include "./beacon_helper/all_purpose.h"
#include "./beacon_helper/can_api.h"

int tilt = 90;
int image_width = 1024;
int image_height = 720;
int have_can = 0;

void *start_behavior(void *args)
{
  camera_forward();

  int** barcodes;
  int count = 0;
  int* xy;

  // Tells the CV library which library to run.
  barcode_run();

  // Configuration for the barcode library
  barcode_configure(2 /*number of digits*/, 10 /*barcodes per pass*/, 100 /*kept passes*/, 
		          1 /*skip pixel width*/, 2 /*min bar width*/, 100 /* Allowed skew */, -1 /*Otsu thresholding*/,
                          image_width /*image width*/, image_height /*image height*/);
  sleep(1);

  // Helper function to determine if a key has been pressed
  while(kbhit()); //clear buffer out
  while(!kbhit())
  {
    // Waits for barcode data
    barcode_frame_wait();
    // Get the barcode data
    barcodes = barcode_get_barcodes();

    int num_codes = (int)barcodes[0]; // Number of barcodes detected
    int num_digits = (int)barcodes[1];
    double dist;
    int i;

    printf("Detected %d barcodes %d digits long:\n", num_codes, num_digits);

    // Cycle through all of the barcodes detected
    for(i = 2; i < num_codes+2; i++)
    {
      int j;
      uint32_t sum = 0;
      printf("\tValue: ");

      // This library outputs the digits detected.  Convert to a decimal value
      for(j = 0; j < num_digits; j++)
      {
	sum += barcodes[i][j] * pow(10,(num_digits-1)-j);
	printf("%d", barcodes[i][j]);
      }

      // Display relevant information.
      printf(" = %d, loc: (%d,%d)->(%d,%d), direction: %d\n",
	sum,
	barcodes[i][j+0], barcodes[i][j+1],
	barcodes[i][j+2], barcodes[i][j+3],
	barcodes[i][j+4]);

      // Display the current (x,y) pos and distance
      xy = barcode_get_cur_xy(sum);
      printf("\t\tcurr x/y loc: (%d,%d)", xy[0], xy[1]);
      dist = dist_to_can(xy[1]);
      free(xy);

      // Display the last (x,y) pos and distance
      xy = barcode_get_last_xy(sum);
      printf("\t\tlast x/y loc: (%d,%d)\n", xy[0], xy[1]);
      free(xy);

      printf("Distance to can: %.2lf\n", dist);
    }

    count++;
    free(barcodes);
  }
  shutdown_run();
  sleep(1);
}
Edit - History - Print - Recent Changes - Search
Page last modified on September 14, 2014, at 08:06 PM