Go Down

Topic: Problem saving an array (Read 798 times) previous topic - next topic

urko_18

Hello everybody,

I am doing a program in arduino and I have a question / problem:

My program controls a robotic arm with 4 dynamixel motors using an ArbotiX-M. I move manually the arm and the program saves the positions of the movements of the motors in an array. Afterwards, the program plays the sequence and the arm moves repeating the sequence that has been saved.

My problem is that the maximum number of positions that I can save are 400, (I mean, 400 positions for each engine, 1600 positions),aand if I put to store more positions the program goes crazy.

So, I would like to know if I am exceeding the limit of any memory, because when compiling what it says is:

"The sketch uses 8,656 bytes (13%) of the program's storage space, the maximum is 65,536 bytes.
Global variables use 806 bytes (19%) of dynamic memory, leaving 3,290 bytes for local variables. The maximum is 4,096 bytes. "

If so, is there a solution ???

many thanks,

Urko.

AWOL

Post.Your. Code.

In code tags
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

urko_18

Code: [Select]

/********************************************************************************
    Program to control Dynamixel of robotic arm by urko_18
********************************************************************************/
#include <ax12.h> //Include ArbotiX DYNAMIXEL library
int kop = 4; //Cuantity of dynamixel
int SERVO_ID[] = {1, 2, 3, 4}; // ID numbers
int regData1, regData2, regData3, regData4;
int turns = 0;
int turns2 = 0;
int turns3 = 0;
int turns4 = 0;
int l, i, repetitions;
double Speed;
/*****(r = 1; m = 420) (r = 2; m = 210) (r = 3; m = 140) (r = 4; m = 105)*****/ // maximun "m" for differents "r"
int m = 400; //columns
int r = 1; //rows


void setup()
{
 dxlInit(1000000); //start dynamixel library at 1mbps to communicate with the servos
 Serial.begin(9600); //start serial at 9600 for reporting data.

 Relax(SERVO_ID[0]);
 Relax(SERVO_ID[1]);
 Relax(SERVO_ID[2]);
 Relax(SERVO_ID[3]);

 Serial.print("ID: ");
 Serial.println(SERVO_ID[0]);
 Serial.print ("ID: ");
 Serial.println(SERVO_ID[1]);
 Serial.print("ID: ");
 Serial.println(SERVO_ID[2]);
 Serial.print("ID: ");
 Serial.println(SERVO_ID[3]);
 delay(1000);

}


void loop()
{

 int positionn1[r][m]; //Matrix of movements
 int positionn2[r][m];
 int positionn3[r][m];
 int positionn4[r][m];

 a:
 Serial.println("How fast do you want to repeat the sequence?");
 Serial.println(" 1 = Slow     2  = Normal     3 = Fast");

 while (Serial.available() == 0) {}
 Speed = Serial.parseInt();

 while (Speed >=4){
 Serial.println("You have entered a wrong value");
 goto a;
 }
 while (Speed ==1){
 Speed=1000;
 }
 while (Speed ==2){
 Speed=700;
 }
 while (Speed ==3){
 Speed=400;
 }
 Serial.print("Speed = ");
 Serial.print(Speed, 0);
 Serial.println(" microseconds");
 delay(3000);
 
 Serial.println("How many times do you want to repeat the sequence?");

 while (Serial.available() == 0) {}
 repetitions = Serial.parseInt();
 Serial.print("repetitions = ");
 Serial.println(repetitions);
 delay(3000);

 for (l = 0; l < r; l++) // structure to create rows
 {
   Serial.print("Positions vector ");
   Serial.print(l + 1);
   Serial.print(": [");

   for (i = 0; i < m; i++) // structure to create columns
   {
     regData1 = dxlGetPosition(SERVO_ID[0]); //read and save the actual position
     regData2 = dxlGetPosition(SERVO_ID[1]);
     regData3 = dxlGetPosition(SERVO_ID[2]);
     regData4 = dxlGetPosition(SERVO_ID[3]);

     positionn1[l] = regData1; //Creation movements vector
     positionn2[l] = regData2;
     positionn3[l] = regData3;
     positionn4[l] = regData4;

     delay(20);

     Serial.print(positionn1[l]); //Display the vector
     Serial.print(", ");
     Serial.print(positionn2[l]);
     Serial.print(", ");
     Serial.print(positionn3[l]);
     Serial.print(", ");
     Serial.print(positionn4[l]);
     Serial.print("; ");
   }
   Serial.print("]\n");
   if (l < (r - 1)) {
     Serial.println("Next movement");
   }
   delay(2000);
 }

 delay(5000);
 Serial.println("The servos will move to the initial position.");

 /***The servos will move according to registered movements***/


 for (int a = 0; a < repetitions; a++) //Repetition of the process (a = number of sequences)
 {
   Serial.print("SEQUENCE ");
   Serial.println(a + 1);

   int before_position_1, before_position_2, before_position_3, before_position_4;
   int position1 = positionn1[0][0];
   int position2 = positionn2[0][0];
   int position3 = positionn3[0][0];
   int position4 = positionn4[0][0];

   for (l = 0; l < r; l++)
   {

     //First, the servos will move to initial position, in order to do the registered movements

     int pos1_1 = dxlGetPosition(SERVO_ID[0]); //Actual servo position
     int pos1_2 = dxlGetPosition(SERVO_ID[1]);
     int pos1_3 = dxlGetPosition(SERVO_ID[2]);
     int pos1_4 = dxlGetPosition(SERVO_ID[3]);
     int pos2_1 = positionn1[l][0]; //Initial position of the movement (objective)
     int pos2_2 = positionn2[l][0];
     int pos2_3 = positionn3[l][0];
     int pos2_4 = positionn4[l][0];

     posiziora_joan(pos1_1, pos1_2, pos1_3, pos1_4, pos2_1, pos2_2, pos2_3, pos2_4); //Function that moves the robot to the initial position

     if (l == 0) {
       Serial.println("Now the servos will do the registered movements.");
     }
     delay(2000);

     //Now, they will move through the registered positions

     int  initial1 = positionn1[l][0];
     int  initial2 = positionn2[l][0];
     int  initial3 = positionn3[l][0];
     int  initial4 = positionn4[l][0];

     for (i = 0; i < m; i++)
     {

       //ID1
       if (positionn1[l] < initial1)
       {
         while (positionn1[l] < initial1)
         {
           before_position_1 = initial1;
           dxlSetGoalPosition(SERVO_ID[0], initial1);
           initial1--;
           delayMicroseconds(Speed);

           if (initial1 != before_position_1)
           {
             if (initial1 == position1 + MX_MAX_POSITION_VALUE)
             {
               position1 = initial1;
               turns++;
               Serial.print("Turns engine 1: ");
               Serial.println(turns);
             }
             else if (initial1 == position1 - MX_MAX_POSITION_VALUE)
             {
               position1 = initial1;
               turns--;
               Serial.print("Turns engine 1: ");
               Serial.println(turns);
             }
           }
         }
       }
       else if (positionn1[l] > initial1)
       {
         while (positionn1[l] > initial1)
         {
           before_position_1 = initial1;
           dxlSetGoalPosition(SERVO_ID[0], initial1);
           initial1++;
           delayMicroseconds(Speed);
           if (initial1 != before_position_1)
           {
             if (initial1 == position1 + MX_MAX_POSITION_VALUE)
             {
               position1 = initial1;
               turns++;
               Serial.print("Turns engine 1: ");
               Serial.println(turns);
             }
             else if (initial1 == position1 - MX_MAX_POSITION_VALUE)
             {
               position1 = initial1;
               turns--;
               Serial.print("Turns engine 1: ");
               Serial.println(turns);
             }
           }
         }
       }

####### THE SAME WITH THE OTHER MOTORS (ID2 ID3 ID4)#######




     Serial.println(" ");
     delay(1000); //Pause between movements
   }
   delay(300); //Pause between sequences

 }
 delay(7000);


 /****REST POSITION****/
 Serial.println("The robot will move to the resting position.");

 int pos1_1 = dxlGetPosition(SERVO_ID[0]); //actual robot position
 int pos1_2 = dxlGetPosition(SERVO_ID[1]);
 int pos1_3 = dxlGetPosition(SERVO_ID[2]);
 int pos1_4 = dxlGetPosition(SERVO_ID[3]);
 int pos2_1 = 3820; //rest position of the robot (the positions of the 4 dynamixel)
 int pos2_2 = 400;
 int pos2_3 = 230;
 int pos2_4 = 2395;

 posiziora_joan(pos1_1, pos1_2, pos1_3, pos1_4, pos2_1, pos2_2, pos2_3, pos2_4);//function

 delay(1000);

 Relax(SERVO_ID[0]);
 Relax(SERVO_ID[1]);
 Relax(SERVO_ID[2]);
 Relax(SERVO_ID[3]);

 Serial.println("END!");

AWOL

#3
Dec 05, 2017, 11:35 am Last Edit: Dec 05, 2017, 11:37 am by AWOL
Code: [Select]
int positionn1[r][m]; //Matrix of movements
 int positionn2[r][m];
 int positionn3[r][m];
 int positionn4[r][m];
It doesn't make sense (to me) to have these a local automatic variables, but if you do the sums, you'll see where your RAM is going.

And then I saw this
Code: [Select]
a:
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

urko_18

Thanks for answering!

So, what is it that you would do with the local automatic variables ?


Regarding the issue of
to:
I do not understand what you want to tell me, it's a goto, does something happen with that?

AWOL

Thanks for answering!

So, what is it that you would do with the local automatic variables ?
Shrink them, and make them global.

Quote
Regarding the issue of
to:
I do not understand what you want to tell me, it's a goto, does something happen with that?
It's a label, and labels usually have an associated goto.
Why are you using goto?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

urko_18

Could you explain or give me an example of how they can be reduced or converted to global?


Regarding the goto, I do not know, it was the first idea that occurred to me so that the program would return to the site of the code that I want. Can you say to mee any other way more simply or better?

Thank you!

Urko.

Go Up