Author Topic: Pgrogramming arduino to act on an array  (Read 1556 times)

0 Members and 1 Guest are viewing this topic.

Offline WhomBomTopic starter

  • Full Member
  • ***
  • Posts: 64
  • Helpful? 0
Pgrogramming arduino to act on an array
« on: September 03, 2009, 02:08:22 PM »
I have the following problem:
 my robot should look around, store what it saw with its sharp en than drive towards the most likeable thing it saw, being likebale means not too narrow, like the leg of a chair and not too big like a door or a wall.
The looking around and storing what it saw i already managed, the sensor reading is stored in an array, 20 different headpositions are stored.
But i dont know how to program:

If you find more than 4 headpositions with distance readings that do not differ too much, ignore it.

if you find less than 2 headpositions with distance readings that do not differ too much, ignore it.

else drive to it, this part i also have...




I think i'll need a sonar to detect the narrow object reliably, but that is a budgetary impossibility right now...

Thanks already for any help!

Offline jaime

  • Jr. Member
  • **
  • Posts: 30
  • Helpful? 1
Re: Pgrogramming arduino to act on an array
« Reply #1 on: September 09, 2009, 02:08:02 PM »
I have the following problem:
 my robot should look around, store what it saw with its sharp en than drive towards the most likeable thing it saw, being likebale means not too narrow, like the leg of a chair and not too big like a door or a wall.
The looking around and storing what it saw i already managed, the sensor reading is stored in an array, 20 different headpositions are stored.
But i dont know how to program:

If you find more than 4 headpositions with distance readings that do not differ too much, ignore it.

if you find less than 2 headpositions with distance readings that do not differ too much, ignore it.

else drive to it, this part i also have...

I think i'll need a sonar to detect the narrow object reliably, but that is a budgetary impossibility right now...

Thanks already for any help!

If I understand you correctly, you're trying to pick the right object based on distance.  You need to be more specific in your object picking criteria.  What is your exact criteria for deciding where to go?  All you've expressed is where "not" to go.  Negative logic is typically harder than straight logic.

For instance, it's easier to understand:

Go to the closest item.

Go to the farthest item.

Than to understand:

Go to the not closest item

Go to the not farthest item

And with the negative logic, you still have to deal with "whats left".  That is if you decide to go to the "not closest" item, you still have to decide which of the other 19 items to go to.  What will your criteria be at that point?

Can you try to be a bit more specific?

jaime

Offline WhomBomTopic starter

  • Full Member
  • ***
  • Posts: 64
  • Helpful? 0
Re: Pgrogramming arduino to act on an array
« Reply #2 on: September 10, 2009, 03:13:56 PM »
Hi,

I want it to go to the nearest thing it sees around it, that is smaller than 30cm.

Later on i want to add a temperature sensor and tell it to ignore things that are  not 37 degrees celsius, but one thing at a time!

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: Pgrogramming arduino to act on an array
« Reply #3 on: September 10, 2009, 07:44:53 PM »
The thing is, it's hard to detect the size of an object. If you want to detect if is 30cm tall, that's easy, use a sensor above that height and another below. If the second sensor detects an object check the other sensor to see if it detects the object. If not, that's your 30cm object. You can also tilt the sensor and try to find out the height of the object that way. To determine the width of an object at whatever distance, then you need to pan the sensor until it reaches first edge of the object, keep panning until the other edge of the object is found, then you read the angles where the panning servo found the edges, you already have the measured distance to the edges, then it's just a bit of math to calculate the width of the object that forms the third side of a triangle with 2 known sides and the angle between them. (Hard to explain in english since I learned math in another language...)
Check out the uBotino robot controller!

Offline jaime

  • Jr. Member
  • **
  • Posts: 30
  • Helpful? 1
Re: Pgrogramming arduino to act on an array
« Reply #4 on: September 11, 2009, 10:06:50 AM »
Hi,

I want it to go to the nearest thing it sees around it, that is smaller than 30cm.

Later on i want to add a temperature sensor and tell it to ignore things that are  not 37 degrees celsius, but one thing at a time!

Because you're tracking attributes of various things.  You ought to create a structure for those attributes.  Below is some sample code to get you started.  This won't run on a `bot, but it will compile and run directly on your computer.  Below the code is the output from the program.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

#define NUM_THINGS 20

/*
 * Support macros for the Thing struct.
 */
#define MIN(a, b) (a < b) ? a : b
#define MAX(a, b) (a > b) ? a : b

#define THING_MIN_TEMP(a, b) MIN(a.temp, b.temp)
#define THING_MAX_TEMP(a, b) MAX(a.temp, b.temp)

#define THING_MIN_HEIGHT(a, b) MIN(a.height, a.height)
#define THING_MAX_HEIGHT(a, b) MAX(a.height, b.height)

#define THING_MIN_DIST(a, b) MIN(a.distance, b.distance)
#define THING_MAX_DIST(a, b) MAX(a.distance, b.distance)

/*!
 * @brief Describes a `thing` that the bot sees.
 */
typedef struct {
  /*!
   * Temperature of the object in celsius.  (Going out on a limb and
   * hoping that char may be signed;  it's implementation specific)
   */
  char temp;

  /*!
   * Height of thing in undefined units.  It's up to you to decide what your
   * unit of measure is.  Because it's in a char, you only get 255 values.
   * (Well 256 if you count 0; assuming a char is 8 bits)
   */
  unsigned char height;

  /*!
   * Distance to object in some undefined unit.  Again, you decide what your
   * unit of measure is.
   */
  unsigned short int distance;
} Thing;


/*!
 * Initialize the array of things.
 */
void
init_things(Thing things[])
{
  int x;
  for (x = 0; x < NUM_THINGS; x++) {
    things[x].temp = rand() % 128;
    things[x].height = rand() % 255;
    things[x].distance = rand() % 32767;
  }
}


/*!
 * Print out all things.
 */
void
print_things(Thing things[])
{
  int x;
  printf("All Things\nnumber, temp, height, distance\n");
  for (x=0; x < NUM_THINGS; x++) {
    printf("%5d, %5d,%5d,%5d\n", x, things[x].temp, things[x].height,
  things[x].distance);
  }
  printf("\n");
}


/*!
 * Return the index of thing that has the maximum temperature.
 */
size_t
max_temp(Thing things[])
{
  size_t x, max_idx = 0;
  char max_temp = things[0].temp;
  for (x = 1; x < NUM_THINGS; x++) {
    if (max_temp < things[x].temp) {
      max_temp = things[x].temp;
      max_idx = x;
    }
  }

  return max_idx;
}


/*!
 * Return the index of the closest thing.
 */
size_t
min_distance(Thing things[])
{
  size_t x, min_idx = 0;
  unsigned short int min_distance = things[0].distance;
  for (x=1; x < NUM_THINGS; x++) {
    if (min_distance > things[x].distance) {
      min_distance = things[x].distance;
      min_idx = x;
    }
  }
  return min_idx;
}


int main() {
  /*
   * Declare 20 Things and fill them in.
   */
  Thing things[NUM_THINGS];
  
  init_things(things);
  print_things(things);

  printf("Thing %d has the maximum temperature.\n", max_temp(things));
  printf("Thing %d is the closest.\n", min_distance(things));
  return 0;
}


Output of the program:


All Things
number, temp, height, distance
    0,     0,  203, 3635
    1,    70,  164,15333
    2,    52,  157,31275
    3,   104,  109,18842
    4,    63,  228, 3259
    5,   124,   53, 6102
    6,    22,  224,29211
    7,    37,  107, 4279
    8,   100,  130,26591
    9,    21,  102, 8237
   10,   105,  246,19216
   11,    94,  204,14898
   12,    56,   74,28185
   13,    38,   89, 9799
   14,    37,   90,21578
   15,    68,  156,24772
   16,    13,  130, 8979
   17,    62,  174,25586
   18,     9,  144, 4106
   19,   119,  221,20235

Thing 5 has the maximum temperature.
Thing 4 is the closest.


If possible, it's better to debug / test your libraries "outside" of the 'bot.  Which is what the code above does.  It's incomplete, and if you wanted to you could provide a better level of abstraction around the "min/max" functions.

Hope that helps!
jaime

Offline WhomBomTopic starter

  • Full Member
  • ***
  • Posts: 64
  • Helpful? 0
Re: Pgrogramming arduino to act on an array
« Reply #5 on: September 13, 2009, 03:53:25 PM »
thanks guys, that helps a lot!

 


Get Your Ad Here