2

Author Topic: C language programming problem, AVR  (Read 4700 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
C language programming problem, AVR
« on: May 03, 2007, 02:43:06 PM »
Ok this is really confusing so Im 100% sure Im doing something dumb . . .

I call Move() in my main code, this is Move():
Code: [Select]
void Move(int direction)
{
int time_position=0;//goes up to 8

direction=1;

//if(direction==1)
//{
unsigned int Rib_1_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_2_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_4_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_5_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Bulk_Left[8]={33,33,33,33,33,33,33,33};
//}

//sort through kinematics for one cycle
while(time_position<8)
{
RIB_1_Fin_Left(Rib_1_Left[time_position]);
RIB_2_Fin_Left(Rib_2_Left[time_position]);
RIB_4_Fin_Left(Rib_4_Left[time_position]);
RIB_5_Fin_Left(Rib_5_Left[time_position]);
BULK_Fin_Left(Bulk_Left[time_position]);
time_position++;
}
}

the above code properly compiles with no problem . . . however, when I remove the commenting out for the IF statement, the code no longer works:

Code: [Select]
void Move(int direction)
{
int time_position=0;//goes up to 8

direction=1;

if(direction==1)
{
unsigned int Rib_1_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_2_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_4_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Rib_5_Left[8]={33,33,33,33,33,33,33,33};
unsigned int Bulk_Left[8]={33,33,33,33,33,33,33,33};
}

//sort through kinematics for one cycle
while(time_position<8)
{
RIB_1_Fin_Left(Rib_1_Left[time_position]);
RIB_2_Fin_Left(Rib_2_Left[time_position]);
RIB_4_Fin_Left(Rib_4_Left[time_position]);
RIB_5_Fin_Left(Rib_5_Left[time_position]);
BULK_Fin_Left(Bulk_Left[time_position]);
time_position++;
}
}

when compiling that code, i get these errors that make no sense to me:
(see attached image)

what dumb mistake am i making?!?

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
Re: C language programming problem, AVR
« Reply #1 on: May 03, 2007, 02:46:08 PM »
You're declaring your variable inside an if clause, which is a no-no if you reference it outside the if clause.

You need to declare all the variables at the beginning of the function (or context), and then assign them inside the if clause.

- Jon

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: C language programming problem, AVR
« Reply #2 on: May 04, 2007, 12:21:27 PM »
wow it only took you 3 minutes to fix that . . .

ok so my problem runs a little deeper now . . .

so lets say i initialize an array like this:
unsigned int Rib_1_Left[8]={33,33,33,33,33,33,33,33};

but then later i want the array to have some other random set of numbers stored:
unsigned int Rib_1_Left[8]={133,23,33,43,23,83,93,133};

without reinitiating it, how would i write that above line?

if I just do:
Rib_1_Left[8]={133,23,33,43,23,83,93,133};
it gives me an error that says "expected expression before '{' token"

i could of course do one liners like this that will work:
Rib_1_Left[0]=133;
Rib_1_Left[1]=23;
Rib_1_Left[2]=33;
but since i must have 16 arrays each of size 8 this would get extremely messy very fast

my google search failed to find answers . . .

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
Re: C language programming problem, AVR
« Reply #3 on: May 04, 2007, 01:09:10 PM »
Okay, so its complicated...

The {1,2,3} syntax can only be used during the initialization of the variable, on the declaration line.

What you really need to do is to have an array in memory with each setup, and copy the contents of the template arrays into your working arrays. Of course, given the numbers you're giving me, you're probably going to run out of RAM really quick, unless you're using one of the huge AVR chips.

If that is the case (low RAM), you can store the arrays in FLASH, and then copy them into to the RAM array from there.

Read the following two pages:

http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_rom_array

http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html#g53ee9e2dec1d5f685d78aa8dc444dccb

Hopefully that will point the way...

All this is courtesy of my brother Dave, BTW.

- Jon

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
Re: C language programming problem, AVR
« Reply #4 on: May 04, 2007, 01:15:32 PM »
My brother says to try something like this:

const unsigned int array1[ 8 ] PROGMEM = { 1, 2, 3, 4, 5, 6, 7, 8 };
unsigned int array2[8];
memcpy_P( array2, array1, sizeof( array2 ));

You may also want to check the size of your values - he says 'int' is 16 bits typically on AVRs...

- Jon

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: C language programming problem, AVR
« Reply #5 on: May 04, 2007, 01:30:50 PM »
Quote
Of course, given the numbers you're giving me, you're probably going to run out of RAM really quick, unless you're using one of the huge AVR chips.
ATmega644 (the big DIP package). How do I check what my memory usage is? My PIC compiler tells me, but I havent figure out how with AVR yet . . .

Quote
memcpy_P( array2, array1, sizeof( array2 ));
Just to make sure I understand this correctly, this function takes the contents of array 1 stored in ROM and dumps it into array2 stored in RAM?

Quote
You may also want to check the size of your values - he says 'int' is 16 bits typically on AVRs...
All values in the array will always be between 30 and 70.

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
Re: C language programming problem, AVR
« Reply #6 on: May 04, 2007, 02:49:22 PM »
You've got 64KB of FLASH, and 4KB of RAM...

How many of these 8-byte arrays do you have in total (including all sets of values)?

Yes, that function copies the bytes from the array in FLASH to the array in RAM.

I don't know about how to tell how much RAM is getting used...

- Jon

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: C language programming problem, AVR
« Reply #7 on: May 07, 2007, 01:41:53 PM »
I am getting an error on the very first line of my code.

very first line:
const unsigned int Rib_1_Left[8] PROGMEM = { 1, 2, 3, 4, 5, 6, 7, 8 };

error: expected '=', ',', ';', 'asm' or '__attribute__' before 'PROGMEM'


Looking at the links you sent me, I notice they use PGM_P but Im not . . .

Quote
How many of these 8-byte arrays do you have in total (including all sets of values)?
I am basically storing in pre-programmed motions for my robot. I ideally would need at least 10 arrays, each of size 8. Perferably 3x as many, but Im flexible . . . Any suggestions on reducing the required memory cost?

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
Re: C language programming problem, AVR
« Reply #8 on: May 07, 2007, 02:11:39 PM »
Well, if you only need 10 arrays of 8 bytes each, that's only 80 bytes. 3x that is still only 240 bytes. Just put them in RAM and don't worry about it. You've got 4096 bytes, although you'll be using some of that for stack and other variables.

I thought you would need 10 arrays for each limb...

- Jon

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: C language programming problem, AVR
« Reply #9 on: May 07, 2007, 02:22:48 PM »
so ummmm, that brings me back to my first question . . . how do I change the contents of the array?
sorry for being a pain! im a mechanical engineer :P

Quote
so lets say i initialize an array like this:
unsigned int Rib_1_Left[8]={33,33,33,33,33,33,33,33};

but then later i want the array to have some other random set of numbers stored:
unsigned int Rib_1_Left[8]={133,23,33,43,23,83,93,133};

without reinitiating it, how would i write that above line?

if I just do:
Rib_1_Left[8]={133,23,33,43,23,83,93,133};
it gives me an error that says "expected expression before '{' token"

i could of course do one liners like this that will work:
Rib_1_Left[0]=133;
Rib_1_Left[1]=23;
Rib_1_Left[2]=33;
but since i must have 16 arrays each of size 8 this would get extremely messy very fast

the only other thought i have is that i can do a while loop that does an i++ and individually transfers bytes one by one to the array . . . but thats too processor intensive and too much code i think for something that should be simple . . .

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
Re: C language programming problem, AVR
« Reply #10 on: May 07, 2007, 02:48:17 PM »
memcpy

- Jon

 


Get Your Ad Here

data_list