Memory issue

I have a robot with 12 servos. I have implemented a function move( ) which takes 12 inputs and is designed to move the servos to the 12 input angles smoothly. I have tested this function extensively and it works well when called once.

I would like to write a program that calls this move function repeatedly in the loop section, in order to move the servos through a sequence of positions indexed by 12-dimensional vectors. This works for a small number of calls, but when I call the move function more than about 8 or 9 times it starts to fail and gives inconsistent results.

I believe the issue is that Arduino is running out of SRAM. I have tried to use the PROGMEM function to record the list of 12-dimensional vectors in program memory. Then I tried to set up 12 buffers in the loop and to successively set the buffers to a vector from program memory in the following way.

strcpy_P(buffer1, (char *)pgm_read_word(&(v9[0])))

This would be the code setting the first buffer to the 9th vector. When I do this and Serial.println the buffers it gives the correct values. However, it does not feed them correctly to the move function and again I get inconsistent results.

I would appreciate advice on the correct way to import data from the program memory without exhausting the SRAM as is apparently happening with my code.

Please post your code; please use code tags when doing so.

It's my understanding that you copy everything from PROGMEM to RAM in loop(); in which case you defeat the purpose of using PROGMEM. You should copy one 'vector' from PROGMEM to RAM, use it, copy the next 'vector' to the same variable and use it etc.

Do the values to be written to the servos change in a regular or predictable way ?

I’ll get Ouija board ready

Ouija board

A new Arduino product ? :slight_smile:

Simple, user-friendly interface, and very good for remote debugging.

Memory issues are nothing you cant debug.it is usually you trying to fit something over the size into somewhere... So following Informations are required please:

  • your 12dimensional array... How large is it? And which data type does it have?
  • your arduino board Model (it Makes a huge difference if you are using Uno or mega f.e)
  • your Code.
  • and most importantly when dealing with servos: how Do you connect them to your Board...

And Ouija boards never runs out of battery :)

|500x369

Here is my code. It is too long to fit in the post. It is very simple, it is long just because many things have to be repeated twelve times.

https://drive.google.com/file/d/1sd-dUecVN0XzEGxhDUz03ffAyZdutgRh/view?usp=sharing

This code produces the output 140 170 on the serial monitor. From this we can see that it is not an issue with the mechanics of the servos, as this is the wrong result on the serial monitor.

The servos do not change in a regular of predictable way when I vary the input angles.

I am trying to run this on an Arduino Nano. It may be that this code is simply too complicated to run on the Nano.

It is very simple, it is long just because many things have to be repeated twelve times.

perfect opportunity to learn how to use arrays…

this should raise a warning in the compiler

  const int max2U = max(max2U, change2U);

as max2U is definitely not defined at that stage. Is this correct or should that read

const int max2U = max(max2M, change2U);

to be similar to the others?

the code as indented with random blank lines

for (counter = 1; counter <= maxchange; counter++)

  {



    // update leg1U

    if (change1U > 0) {


      int mod1U; mod1U = maxchange / change1U;



      if (
        (a1U > leg1U.read()) &&
        (
          counter % mod1U
          == 0
        )


      )
        leg1U.write(leg1U.read() + 1);

makes it totally impossible to read on a smartphone.

Why do you store NUMBERS as ASCII text in PROGMEM/flash to then transform them into numbers with atoi() ?? this is nuts… :slight_smile: (sorry not you, doing it that way)/

Seems they are all positive and below 255, so there is plenty of space to store those directly as arrays of uint8_t (aka byte) and not mess around with PROGMEM at all. you program is not that memory hungry. - just change

const char pos1_1U[] PROGMEM = "112";
const char pos1_1M[] PROGMEM = "140";
const char pos1_1L[] PROGMEM =   "0";

const char pos1_2U[] PROGMEM = "109";
const char pos1_2M[] PROGMEM = "140";
const char pos1_2L[] PROGMEM =  "20";

const char pos1_3U[] PROGMEM =  "25";
const char pos1_3M[] PROGMEM = "120";
const char pos1_3L[] PROGMEM =  "20";

const char pos1_4U[] PROGMEM = "162";
const char pos1_4M[] PROGMEM = "140";
const char pos1_4L[] PROGMEM =   "0";

const char* const v1[] PROGMEM = {pos1_1U, pos1_1M, pos1_1L, pos1_2U, pos1_2M, pos1_2L, pos1_3U, pos1_3M, pos1_3L, pos1_4U, pos1_4M, pos1_4L};

intoconst uint8_t v1[] = {112,140,0,109,140,20,25,120,20,162,140,0};

and instead of doing

  strcpy_P(buffer1U, (char*)pgm_read_word(&(v1[0])));
  strcpy_P(buffer1M, (char*)pgm_read_word(&(v1[1])));
  strcpy_P(buffer1L, (char*)pgm_read_word(&(v1[2])));

  strcpy_P(buffer2U, (char*)pgm_read_word(&(v1[3])));
  strcpy_P(buffer2M, (char*)pgm_read_word(&(v1[4])));
  strcpy_P(buffer2L, (char*)pgm_read_word(&(v1[5])));

  strcpy_P(buffer3U, (char*)pgm_read_word(&(v1[6])));
  strcpy_P(buffer3M, (char*)pgm_read_word(&(v1[7])));
  strcpy_P(buffer3L, (char*)pgm_read_word(&(v1[8])));

  strcpy_P(buffer4U, (char*)pgm_read_word(&(v1[9])));
  strcpy_P(buffer4M, (char*)pgm_read_word(&(v1[10])));
  strcpy_P(buffer4L, (char*)pgm_read_word(&(v1[11])));




  move(atoi(buffer1U), atoi(buffer1M), atoi(buffer1L), atoi(buffer2U), atoi(buffer2M), atoi(buffer2L), atoi(buffer3U), atoi(buffer3M), atoi(buffer3L), atoi(buffer4U), atoi(buffer4M), atoi(buffer4L));

just change your move function to take an array and domove(v1);

and what’s going on with the formulas?

const int change1L = sqrt((a1L - leg1L.read()) * (a1L - leg1L.read()));
  const int change1M = sqrt((a1M - leg1M.read()) * (a1M - leg1M.read()));
  const int change1U = sqrt((a1U - leg1U.read()) * (a1U - leg1U.read()));

calculating the square root of the square of a value is your efficient way of getting its absolute value ?

→ your code will suddenly become way shorter if you perform some clean up… :slight_smile:

Yes indeed. It is always the fault of Board when a question comes in this very forum. It is never a mistake the Person coding did…

I am on mobile, so I can’t download and open your Code… You can copy paste your Code inside Code Tags, please.

And then there is this:
Your first post: When I do this and Serial.println the buffers it gives the correct values.

Your last post: as this is the wrong result on the serial Monitor.

pjburton:
I am trying to run this on an Arduino Nano. It may be that this code is simply too complicated to run on the Nano.

just for the fun I gave it a try to use arrays.

==> here is a version of what I think your code was doing. I think it’s a bit shorter :slight_smile:

/*
    Copyright (c) 2020 J-M-L  https://forum.arduino.cc/index.php?action=profile;u=438300
    Author        :   J-M-L
    Create Time   :   May 2020
    Change Log    :

    The MIT License (MIT)
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.

*/

// define  how the robot is built
const uint8_t nbJointsPerLeg = 3;
const uint8_t nbLegs = 4;

// and which pins control which servo
const uint8_t jointPins[nbLegs][nbJointsPerLeg] = {{7, 4, 2}, {8, 9, 10}, {14, 15, 16}, {11, 12, 13}};

// define specific positions by their joint angle
#define v1    {{112, 140,   0}, {109, 140,  20}, { 25, 120,  20}, {162, 140,   0}}
#define v2    {{112, 120,  20}, {109, 120,  20}, { 25, 120,  20}, {162, 120,   0}}
#define v3    {{112, 120,  20}, {109, 120,  20}, { 25, 145,  20}, {162, 120,  20}}
#define v4    {{112, 120,  20}, {109, 120,  20}, { 25, 180,   0}, {162, 120,  20}}
#define v5    {{112, 120,  20}, {109, 120,  20}, {100, 180,   0}, {162, 120,  20}}
#define v6    {{112, 120,  20}, {109, 120,  20}, {100, 160,  45}, {162, 120,  20}}
#define v7    {{112, 120,  20}, {109, 120,  20}, {100, 100,  45}, {162, 120,  20}}
#define v8    {{142, 100,  45}, {164, 120,  20}, { 70, 140,  20}, {107, 120,  20}}
#define v9    {{142, 170,  45}, {164, 120,  20}, { 70, 140,  20}, {107, 120, 170}}
#define v10   {{ 67, 170,  45}, {164, 120,  20}, { 70, 140,  20}, {107, 120,  20}}
#define v11   {{ 67, 120,  45}, {164, 120,  20}, { 70, 140,  20}, {107, 120,  20}}
#define down1 {{ 67, 140,   0}, {164, 140, 178}, { 70, 140,   0}, {107, 140,   0}}
#define down2 {{ 67, 140,   0}, {164, 140,   0}, { 70, 140,   0}, {107, 140,   0}}

// define the sequence you want to play
const uint8_t sequence[][nbLegs][nbJointsPerLeg] = {v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, down1, down2};

// *****************************
#include <Servo.h>

struct t_leg {
  Servo joint[nbJointsPerLeg];
};

struct t_robot {
  t_leg leg[nbLegs];
};

t_robot robot;

const uint8_t nbStepsInSequence = sizeof(sequence) / sizeof(sequence[0]);

void move(const uint8_t destinationPostion[nbLegs][nbJointsPerLeg])
{
  int16_t maxChange = 0;
  uint8_t startPostion[nbLegs][nbJointsPerLeg];

  // calculate the largest move
  for (uint8_t l = 0; l < nbLegs; l++) {
    for (uint8_t j = 0; j < nbJointsPerLeg; j++) {
      startPostion[l][j] = robot.leg[l].joint[j].read();
      int16_t d = (int16_t) destinationPostion[l][j] - (int16_t) startPostion[l][j];
      if (d < 0) d = -d;
      if (d > maxChange) maxChange = d;
    }
  }

  // if there is no move needed, just return
  if (maxChange == 0) return; 

  // make small steps to reach the new position for each joint
  for (int16_t aStep = 1; aStep <= maxChange; aStep++) {
    for (uint8_t j = 0; j < nbJointsPerLeg; j++) {
       for (uint8_t l = 0; l < nbLegs; l++) {
        int16_t deltaMove =  aStep * (((int16_t) destinationPostion[l][j]) - ((int16_t) startPostion[l][j])) / maxChange;
        robot.leg[l].joint[j].write(((int16_t) startPostion[l][j]) + deltaMove);
      }
    }
  }
}

void setup()
{
  Serial.begin(115200);

  // assign pins to the servos
  for (uint8_t l = 0; l < nbLegs; l++)
    for (uint8_t j = 0; j < nbJointsPerLeg; j++)
      robot.leg[l].joint[j].attach(jointPins[l][j]);
}

void loop()
{
  for (uint8_t s = 0; s < nbStepsInSequence; s++) {
    move(sequence[s]);
    delay(5);
  }
}

of course totally untested but that could give you some ideas.

dr-o: I am on mobile, so I can't download and open your Code... You can copy paste your Code inside Code Tags, please.

That file is 28 kB or so; will not work ;)

it definitely does not need to be 28KB...

My attempt is 4K and the MIT licence is a big part of it :)

J-M-L:
it definitely does not need to be 28KB…

Amen.
That is some of the most insanely convoluted code I’ve seen in a looooong time!