Go Down

Topic: writing an expression for box spiral robot (Read 810 times) previous topic - next topic

geoland

Jul 31, 2011, 01:33 pm Last Edit: Aug 04, 2011, 08:12 pm by geoland Reason: 1
I want a simple stepper driven robot device to push buttons (cross configuration) on a hand controller, controlling two perpendicular axis. Thinking 2D,  turning right and moving forward only.

An arm attached to the stepper shaft sweeps over the pushbuttons activating them. To move forward two steps the arm sweeps left then right and vice versa. To turn right, the arm sweeps over two adjacent buttons, going clockwise.

The sequence to do this is as follows - where -1 == left sweep and 1 == right sweep. 2 ==  right and right again. The arm travels 90deg with each sweep.

Each sequence is repeated before being incremented and actioned twice - and so on in the following pattern - a box spiral.

2, -1, 2, -1, 2, -1, 1, -1, 2, -1, 1, -1, 2, -1, 1, -1, 1, -1, 2, -1,1,-1,1,-1, 2,...

I am baffled as to how to express this to run as a for or while statement or other expression.

Why am I doing this? Besides moving an imaging sensor between shots to increase signal to noise ratio in the finished image, the instrument is practically a collectors item. It has a +ve earth! And I don't want to affect it cosmetically.

A bit left field, but thanks for looking.

Rowland.




wildbill

#1
Jul 31, 2011, 03:19 pm Last Edit: Jul 31, 2011, 03:21 pm by wildbill Reason: 1
Could you give a bit more detail about how this works and how that sequence you've provided breaks out into repeated subsequences. I'm struggling to see what it does.

edit: a picture might help

David Pankhurst

This will repeat the sequence you showed:

Code: [Select]
void doSomethingWithIt(int m)
{
  Serial.println(m);
}
void sequence(void)
{
  int seq2=1;
  while (1)
  {
    for (int i=0;i<2;++i)
    {
      doSomethingWithIt(2);
      for (int k=0;k<seq2;++k)
        doSomethingWithIt( ( k&1 ? 1 : -1 ) );
    }
    seq2+=2;
  }
}


Just replace doSomethingWithIt() internals with your control code.

David Pankhurst

Of course, maybe you want to call a function and get a single value back - here's that:
Code: [Select]

int sequence2(int reset)
{
 static seq2=-1, i=99, k=99;
 if (reset)
 {
   i = k = 99;
   return 0; // dummy value - ignore
 }
 if (k>=seq2)
 {
   ++i;
   k=-1;
 }
 if (i>=2)
 {
   i=0;
   k=-1;
   seq2+=2;
 }
 if (k++<0)
 {
   return 2;
 }
 return ( k&1 ? -1 : 1 );
}


Every time you call sequence2(0); it will return the next number in your sequence - if you want to restart back at the beginning again, then call it with sequence2(1) once, then continue with sequence2(0) from then on for the sequence

geoland

Thanks, once again, David. I will look at fitting these options into the existing sketch this week.

Rowland


Go Up