1

There are no errors when compiling/uploading to MCU. The desired behavior is for the motors to move. The difference is creating each motor control/command into compartmentalized functions.

I made declarations and divided the various motor commands (forward, backward, etc) into functions.

I've interchanged the contents of void startup() in and out of main(). I'm confused on why just transferring the contents of main() into functions did not work. It compiles and runs when everything is in main, and when each motor command (backward, forward, etc) is put into isolated functions, it only compiles. It does not cause the motors to actually move when loaded onto the board.

Did I do some incorrect ordering? I defined L6470 **motors as a global variable. Do some more things need to be defined as a global variables? If this C++ program cannot be broken up into functions, why not?

Here is a minimal reproducible example, broken up into one function:

#include "mbed.h"
#include "DevSPI.h"
#include "XNucleoIHM02A1.h"

#define DELAY_1 1000

   L6470 **motors;

/* Motor Control Expansion Board. */
XNucleoIHM02A1 *x_nucleo_ihm02a1;

void forward(); // declaration 

int main()
{
    /* Initializing SPI bus. */
#ifdef TARGET_STM32F429
    DevSPI dev_spi(D11, D12, D13);
#else
    DevSPI dev_spi(D11, D12, D13);
#endif

    /* Initializing Motor Control Expansion Board. */
    x_nucleo_ihm02a1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi);

    L6470 **motors = x_nucleo_ihm02a1->get_components();

    motors[0]->set_home();    
    wait_ms(DELAY_1);    
    position = motors[0]->get_position();

    forward();
}  

void forward()
{
        motors[0]->move(StepperMotor::FWD, STEPS_1);
        motors[0]->wait_while_active();    
        position = motors[0]->get_position();
        motors[0]->set_mark();    
    wait_ms(DELAY_1);
}

If you don't think this question is clear, please suggest why.

adamaero
  • 220
  • 3
  • 17

2 Answers2

3

Because you never initialized your global variable, but instead created an additional local variable your function does not know about:

This:

L6470 **motors = x_nucleo_ihm02a1->get_components();

needs to be

motors = x_nucleo_ihm02a1->get_components();

to use your global variable.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • =o I didn't know a local variable could overwrite...or just ignore a global variable of the same name... – adamaero Aug 06 '19 at 14:48
  • 3
    @AdamUraynar It isn't just global vs local variables. Any variable of inner scope, can shadow the variable, of the same name, in the outer scope. As in: `int main () {int i = 1; {int i = 2; {int i = 3; {int i = 4; std::cout << i;}}}}` would print `4`. – Algirdas Preidžius Aug 06 '19 at 14:51
  • 3
    It's commonly called [shadowing](https://en.wikipedia.org/wiki/Variable_shadowing). You could check whether your compiler toolchain offers any tooling to at least have this produce a warning. – nvoigt Aug 06 '19 at 14:51
  • @LightnessRacesinOrbit This is not my program: https://os.mbed.com/components/X-NUCLEO-IHM02A1/ – adamaero Aug 06 '19 at 15:02
  • I cannot see your code on that page or in the samples linked from that page. – Lightness Races in Orbit Aug 06 '19 at 15:03
  • @LightnessRacesinOrbit It uses Mbed's online compiler, no debugger (after importing "Hello World" program). I was just mentioning that I didn't create the original in the first place. – adamaero Aug 06 '19 at 15:13
2

You declared a global variable motors. Then you decided one isn't good enough and declared another variable named motors, local in main. You have initialised that second variable, perhaps correctly. The global variable remains zero iniitialised, and when your other function tries to access it, you get undefined behaviour.

As a tangentially related piece of advice, avoid global variables altogether.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243