4 Replies Latest reply on Apr 20, 2016 3:29 PM by Guimficus

    How to solve mraa multiple definitions error?

    Guimficus

      Hi all,

       

      I use Edison with Arduino Board and I'm developing a project in c++ with Eclipse. But, as I have more than one file I've a problem to share mraa object between my files. I've declare my pins to drive a stepper motor in my .hpp file (Header.hpp) as below:

       

      #ifndef HEADER_HPP_

      #define HEADER_HPP

      .....................................................

      ...................code.................................

      .....................................................

      mraa_gpio_context pin_5;

      mraa_gpio_context pin_7;

      mraa_aio_context pin_A2;

      mraa_gpio_context pin_3;

      mraa_gpio_context pin_11;

      ..................................................

      ......................................

      .............................................

      #endif /* HEADER_HPP_ */

       

      So, i've #include it in my .cpp files were I want to define an check them. But after debug I've the follow errors:

      multiple definition of pin_5

      multiple definition of pin_7

      multiple definition of pin_A2

      multiple definition of pin_3

      multiple definition of pin_11

       

      Someone can tell me what have I done wrong and how can I do to solve this problem?

      Thanks

        • 1. Re: How to solve mraa multiple definitions error?
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hello Guimficus,
           
          This issue usually happens when you declare variables more than once in your code. I ran an example following your configuration to see if I got the same results as you, however I was not able to do so. Could you share your .cpp and your .hpp to see if we can find what's causing the issue?
           
          Peter.

          • 2. Re: How to solve mraa multiple definitions error?
            Guimficus

            Hello Peter,

             

            Thanks to help me. I think you've right about the cause of this problem, but I don't know how to solve it. I'm just starting with the mraa library. Here are the contain of my files. Sorry but there is no icon to join my files, so I've just copy them. You can give me your email if you really want the files.

             

            **********main file: Soft_Edison.cpp***************

            #include <iostream>
            #include "Header.hpp"
            #include "Stepper.hpp"
            using namespace std;
            int main() {
              /* Setup your example here, code that should run once
               */
            initialisation();
            moteurMarche();
              /* Code in this loop will run repeatedly
               */
              for (;;) {
                  sensTrigo();
              }
              return 0;
            }
            

             

            ***************Stepper.hpp*****************

            /*
            * Stepper.hpp
            *
            *  Created on: 18 avr. 2016
            *      Author: Utilisateur
            */
            #ifndef STEPPER_HPP_
            #define STEPPER_HPP_
            #include "mraa.hpp"
            
            mraa_gpio_context motpin_1;
            mraa_gpio_context motpin_2;
            // library interface description
            class Stepper {
              public:
                // constructors:
                Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2);
            
                // speed setter method:
                void setSpeed(long whatSpeed);
            
                // mover method:
                void step(int number_of_steps);
            
              private:
                void stepMotor(int this_step);
            
                int direction;            // Direction of rotation
                unsigned long step_delay; // delay between steps, in ms, based on speed
                int number_of_steps;      // total number of steps this motor can take
                int step_number;          // which step the motor is on
            
                // motor pin numbers:
                int motor_pin_1;
                int motor_pin_2;
                unsigned long last_step_time; // time stamp in us of when the last step was taken
            };
            
            #endif /* STEPPER_HPP_ */
            

             

            *********************Header.hpp******************************************

            /*
            * Header.hpp
            *
            *  Created on: 18 avr. 2016
            *      Author: Utilisateur
            */
            #ifndef HEADER_HPP_
            #define HEADER_HPP_
            
            #include "mraa.hpp"
            //mode debug
            #define debug true;
            
            //--------------------------------------------------------------------------------//
            //                            Etats des broches                                   //
            //--------------------------------------------------------------------------------//
            #define HIGH true
            #define LOW false
            
            //Define output
            #define CTRLPIN_A         3               // Enable (PWM)A on Motor Control Shield
            #define CTRLPIN_B         11              // Enable (PWM)B on Motor Control Shield
            #define dirA        12              // Direction pin dirA on Motor Control Shield
            #define dirB        13              // Direction pin dirB on Motor Control Shield
            
            //Define constantes moteur
            #define MOT_ZERO 14
            #define MOT_ERREUR_COURSE 2
            #define MOT_COURSE_AGITATION 100
            #define direct true;
            #define indirect false;
            #define capteurPosition1 5
            #define capteurPosition2 7
            
            //Tête optique
            #define capteurOptique   2
            
            //Broche de remise à zéro du moteur
            #define RESETLINE 4  // Change this if you are not using an Arduino Adaptor Shield R3
            
            //--------------------------------------------------------------------------------//
            //                            Hardware resources allocation                       //
            //--------------------------------------------------------------------------------//
            mraa_gpio_context pin_5;
            mraa_gpio_context pin_7;
            mraa_aio_context pin_A2;
            mraa_gpio_context pin_3;
            mraa_gpio_context pin_11;
            
            // Fonction inclu dans init.cpp
            void initialisation(void);
            #endif /* HEADER_HPP_ */
            

             

            *******************Init.cpp*********************************

            /*
            * Init.cpp
            *
            *  Created on: 18 avr. 2016
            *      Author: Utilisateur
            */
            #include "Header.hpp"
            #include "mraa.hpp"
            
            void initialisation(void)
            {
            
                //Serial.begin(115200);
                //Serial1.begin(115200);    // serial1 to communicate with the screen
                mraa_init();
                pin_5 = mraa_gpio_init(capteurPosition1);
                pin_7 = mraa_gpio_init(capteurPosition2);
                pin_A2 = mraa_aio_init(capteurOptique);
                pin_3 = mraa_gpio_init(CTRLPIN_A);
                pin_11 = mraa_gpio_init(CTRLPIN_B);
                mraa_gpio_dir(pin_5, MRAA_GPIO_IN);
                mraa_gpio_mode(pin_5, MRAA_GPIO_PULLUP);
                mraa_gpio_dir(pin_7, MRAA_GPIO_IN);
                mraa_gpio_mode(pin_7, MRAA_GPIO_PULLUP);
                mraa_gpio_dir(pin_3, MRAA_GPIO_OUT);
                mraa_gpio_dir(pin_11, MRAA_GPIO_OUT);
                return;
            }
            

             

            ********************Stepper.cpp**********************************

            //
            #include "Header.hpp"
            #include "Stepper.hpp"
            #include <time.h> // for clock
            
            clock_t t1;
            
            Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
            {
              this->step_delay = 0;        //delay between steps, in ms
              this->step_number = 0;    // which step the motor is on
              this->direction = 0;      // motor direction
              this->last_step_time = 0; // time stamp in us of the last step taken
              this->number_of_steps = number_of_steps; // total number of steps for this motor
            
              // Arduino pins for the motor control connection:
              this->motor_pin_1 = motor_pin_1;
              this->motor_pin_2 = motor_pin_2;
            
              // setup the pins on the microcontroller:
              motpin_1 = mraa_gpio_init(motor_pin_1);
              motpin_2 = mraa_gpio_init(motor_pin_2);
              mraa_gpio_dir(motpin_1, MRAA_GPIO_OUT);
              mraa_gpio_dir(motpin_2, MRAA_GPIO_OUT);
            }
            
            void Stepper::setSpeed(long whatSpeed)
            {
              this->step_delay = 60L * 1000L * 1000L / this->number_of_steps / whatSpeed;
            }
            
            /*
            * Moves the motor steps_to_move steps.  If the number is negative,
            * the motor moves in the reverse direction.
            */
            void Stepper::step(int steps_to_move)
            {
              int steps_left = abs(steps_to_move);  // how many steps to take
            
              // determine direction based on whether steps_to_mode is + or -:
              if (steps_to_move > 0) { this->direction = 1; }
              if (steps_to_move < 0) { this->direction = 0; }
            
            
              // decrement the number of steps, moving one step each time:
              while (steps_left > 0)
              {
                t1=clock();
                unsigned long now = (unsigned long)t1;
                // move only if the appropriate delay has passed:
                if (now - this->last_step_time >= this->step_delay)
                {
                  // get the timeStamp of when you stepped:
                  this->last_step_time = now;
                  // increment or decrement the step number,
                  // depending on direction:
                  if (this->direction == 1)
                  {
                    this->step_number++;
                    if (this->step_number == this->number_of_steps) {
                      this->step_number = 0;
                    }
                  }
                  else
                  {
                    if (this->step_number == 0) {
                      this->step_number = this->number_of_steps;
                    }
                    this->step_number--;
                  }
                  // decrement the steps left:
                  steps_left--;
                  // step the motor to step number 0, 1, ..., {3 or 10}
                  stepMotor(this->step_number % 4);
                }
              }
            }
            
            /*
            * Moves the motor forward or backwards.
            */
            void Stepper::stepMotor(int thisStep)
            {
                  switch (thisStep) {
                  case 0:  // 01
                    mraa_gpio_write(motpin_1, LOW);
                    mraa_gpio_write(motpin_2, HIGH);
                  break;
                  case 1:  // 11
                      mraa_gpio_write(motpin_1, HIGH);
                      mraa_gpio_write(motpin_2, HIGH);
                  break;
                  case 2:  // 10
                      mraa_gpio_write(motpin_1, HIGH);
                      mraa_gpio_write(motpin_2, LOW);
                  break;
                  case 3:  // 00
                      mraa_gpio_write(motpin_1, LOW);
                      mraa_gpio_write(motpin_2, LOW);
                  break;
                }
            }
            

             

            *********************************Motor_driver.cpp***************************

            /*
            * Motor_driver.cpp
            *
            *  Created on: 18 avr. 2016
            *      Author: Utilisateur
            */
            #include "Header.hpp"
            #include "Stepper.hpp"
            
            int vitesse = 60;                   // set the speed at 60 rpm:
            int stepsPerRevolution = 200;       // Number of steps per revolution (360/1.8)
            
            // Initialize the stepper library
            Stepper myStepper(stepsPerRevolution, dirA, dirB);
            
            bool testMoteur(unsigned int const nbreDePas)
            {
                int stepCount = 0;
                while (mraa_gpio_read(pin_5) == HIGH)
                    myStepper.step(-1);       // Effectuer un pas dans le sens direct
              //Serial.println("capteur1 activée");
                while (mraa_gpio_read(pin_7) == LOW)
                {
              //Serial.println(millis());
                    myStepper.step(1);       // Effectuer un pas dans le sens indirect
                //Serial.println(millis());
                    stepCount++;
                }
              //Serial.println("capteur2 activée");
                usleep(150000);
                myStepper.step(-MOT_ZERO);
                //if (debug) Serial.println(stepCount);
                return (abs(stepCount - nbreDePas) < MOT_ERREUR_COURSE);
            }
            
            void RAZMoteur()
            {
                mraa_gpio_write(pin_3, HIGH);
                mraa_gpio_write(pin_11, HIGH);
                while (mraa_gpio_read(pin_5) == HIGH)
                {
                    myStepper.step(-1);      // Effectuer un pas dans le sens arrière
                }
            
                while (mraa_gpio_read(pin_7) == HIGH)
                {
                    myStepper.step(1);      // Effectuer un pas dans le sens avant
                }
                usleep(150000);
                myStepper.step(-MOT_ZERO);
                return;
            }
            
            void moteurMarche()
            {
                mraa_gpio_write(pin_3, HIGH);
                mraa_gpio_write(pin_11, HIGH);
            }
            
            void sensTrigo(void)
            {
                myStepper.step(-1);      // Effectuer un pas dans le sens trigonométrique
            }
            
            void sensNonTrigo(void)
            {
                myStepper.step(1);      // Effectuer un pas dans le sens non trigonométrique
            }
            
            void moteurRepos()
            {
                mraa_gpio_write(pin_3, LOW);
                mraa_gpio_write(pin_11, LOW);
                return;
            }
            
            • 3. Re: How to solve mraa multiple definitions error?
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              Are these files the latest versions, you are currently using? I'm encountering different issues from what you described, that's why I ask.
              Do you receive any error messages regarding motpin_1 and motpin_2?  I managed to "solve" them but I would like to know if you've made any modifications.

              Peter.

              • 4. Re: How to solve mraa multiple definitions error?
                Guimficus

                Yes, the are the lastest versions of my files. Fortunately, I've been able to solve this problem: a I've declared the motor pins in an other hpp file and I've included it only in one cpp file.

                Thank you for your help!

                 

                Guimficus.