7 Replies Latest reply on Apr 27, 2016 6:55 AM by FGT

    Why is Class not working?

    aramperez

      Hi Folks,

       

      I'm using an Galileo 2, Arduino IDE v1.6.8 and I'm defining a DC_Motor class to drive some DC motors. For simplicity, I'm using 2 LEDs for testing. Below is my code. It does not work unless I comment out the define for USE_MOTOR_CLASS :-(. Here's my simple sketch:

       

      //#define USE_MOTOR_CLASS
      
      #define PIN_A 2
      #define PIN_B 3
      
      #if defined(USE_MOTOR_CLASS)
      class DC_Motor {
          int pinA;
          int pinB;
      
          DC_Motor(); //No default constructor
      
        public:
          DC_Motor(int a, int b) :
            pinA(a),
            pinB(b)
          {
            pinMode(pinA, OUTPUT);
            digitalWrite(pinA, LOW);
            pinMode(pinB, OUTPUT);
            digitalWrite(pinB, LOW);
          }
          void stop() {
            digitalWrite(pinA, LOW);
            digitalWrite(pinB, LOW);
            Serial.println("A:0 B:0");
          }
          void cw() {
            digitalWrite(pinA, LOW);
            digitalWrite(pinB, HIGH);
            Serial.println("A:0 B:1");
          }
          void ccw() {
            digitalWrite(pinA, HIGH);
            digitalWrite(pinB, LOW);
            Serial.println("A:1 B:0");
          }
      };
      
      DC_Motor m(PIN_A, PIN_B);
      #else
      int pinA = PIN_A;
      int pinB = PIN_B;
      
      void stop() {
        digitalWrite(pinA, LOW);
        digitalWrite(pinB, LOW);
      }
      
      void cw() {
        digitalWrite(pinA, LOW);
        digitalWrite(pinB, HIGH);
      }
      
      void ccw() {
        digitalWrite(pinA, HIGH);
        digitalWrite(pinB, LOW);
      }
      #endif
      
      void printMenu()
      {
        Serial.print("\r\n"
                     "=== Menu ===\r\n"
                     "1) Stop\r\n"
                     "2) CW\r\n"
                     "3) CCW\r\n"
                     "Select: ");
      }
      
      void setup() {
      #if !defined(USE_MOTOR_CLASS)
        pinMode(pinA, OUTPUT);
        pinMode(pinB, OUTPUT);
        stop();
      #endif
        Serial.begin(115200);
        Serial.print("\r\n\nGalileo ready");
        printMenu();
      }
      
      void loop() {
        if ( Serial.available() ) {
          int ch = Serial.read();
          Serial.write(ch);
          Serial.println();
          switch ( ch ) {
            case '1':
      #if defined(USE_MOTOR_CLASS)
              m.stop();
      #else
              stop();
      #endif
              break;
            case '2':
      #if defined(USE_MOTOR_CLASS)
              m.cw();
      #else
              cw();
      #endif
              break;
            case '3':
      #if defined(USE_MOTOR_CLASS)
              m.ccw();
      #else
              ccw();
      #endif
              break;
            default:
              Serial.println("** Invalid selection!! **");
          }
          printMenu();
        }
      }
      

       

      Does anyone see what's wrong with the class?

       

      TIA,

      Aram

        • 1. Re: Why is Class not working?
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hello aramperez,

          What exactly happens before and after you comment the line? How is this affecting the sketch? I mean I tried to compile the code on the Arduino IDE and I didn't receive any error message. Because of that I believe your issue is probably not related to the IDE per se. So, could you please explain us a little bit more about the issue?

          Peter.

          • 2. Re: Why is Class not working?
            aramperez

            Hi Peter,

             

            The program compiles fine with line 1 uncommented or with line 1 commented. Connect 2 LEDs to pins 2 (L2) and 3 (L3), and connect the serial console. Compile/download the program to the Galileo G2 with line 1 commented out. Both LEDs should be off and you should see a menu with 3 options (on the serial console). Select option 2 and L3 will light up (and L2 will remain off). Then select option 3 and L3 turns off and L2 turns on. Select option 1 and both LEDs should be off. Now uncomment line 1 and compile/download the program. Select option 2 and L3 will light up. Then selection option 3 and L3 stays on and L2 turns off. Select option 1 and both LEDs remain on.

             

            My question if Why does this happen if the code is virtually identical?

             

            Thanks,

            Aram

            • 3. Re: Why is Class not working?
              FGT

              Hi aramperez,

               

              I can't really explain why... I think it is related to object creation and code executed at class constructors... I tried changing a little bit your example: print values read form the actual pins and the expected ones and adding a line at setup:

              m = DC_Motor(PIN_A, PIN_B);   

              for creating m at setup(), attached code. I confirmed your results about something is not working with class, i.e. running basically your code:

              and it is working as expected without class:


              If the assignment is added at setup, the code works "as expected":

              Maybe it's some combination of class - constructor - construction code at runtime - etc.


              HTH,


              Fernando.

              • 4. Re: Why is Class not working?
                aramperez

                Hi Fernando,

                 

                Sorry but I'm traveling for a wedding and haven't had time to catch up on my emails.

                 

                I'll change my code to how you got it to work but I shouldn't have to. Who/where should I file an error report against the compiler?

                 

                Thanks,

                Aram

                • 5. Re: Why is Class not working?
                  FGT

                  aramperez wrote:

                   

                  Hi Fernando,

                  ...

                  I'll change my code to how you got it to work but I shouldn't have to. Who/where should I file an error report against the compiler?

                   

                  I think in some Arduino forum... but maybe you should verify the code/compiler behavior with an Arduino card first.

                   

                  Fernando.

                  • 6. Re: Why is Class not working?
                    aramperez

                    I'll post something on the Arduino site. However, isn't Intel who provides the compiler, or at least, makes sure it works with the Quark chip?

                     

                    /Aram

                    • 7. Re: Why is Class not working?
                      FGT

                      aramperez wrote:

                       

                      I'll post something on the Arduino site. However, isn't Intel who provides the compiler, or at least, makes sure it works with the Quark chip?

                       

                      /Aram

                       

                      If there is a problem and it is found using Arduino, then I think it's an Arduino problem, that maybe Intel didn't solve/see/etc. (maybe Intel focus is not on debugging third party software, idk).

                       

                      If there is a problem and it is not found using Arduino, then I think it's an Intel problem.

                       

                      Disclaimer: I'm completely unrelated to Arduino IDE development and its Intel adaptation/tailoring to Galileo.

                       

                      Fernando.