7 Replies Latest reply on Mar 16, 2016 6:04 PM by PabloM_Intel

    Eclipse IDE: Digital write

    dunex_1

      I am trying to read a digital value from my playstation controller to use it to run a motor using a motor driver. The code below is what I have implemented

       

      /*

      *  Example for DS4driver

      */

      #include "mraa.hpp"

      #include <iostream>

      #include <unistd.h>

      #include "DS4driver.h"

       

      using namespace std;

       

      void floatdelay(float delay){

        clock_t start, stop;

        start = clock();

        stop  = clock();

        while(((float)(stop - start)/ CLOCKS_PER_SEC) < delay){

        stop = clock();

        }

      }

      void commandMotors(int LY, int RY){

        mraa::Gpio* Motor1 = new mraa::Gpio(3);

        mraa::Gpio* Motor2 = new mraa::Gpio(5);

       

        if(LY <= 120){

        cout << "Hello" << endl;

        Motor1->write(LY);

        }

        if(LY >= 135){

        Motor1->write(LY);

        }

        if(RY <= 120){

        Motor2->write(RY);

        }

        if(RY >= 135){

        Motor2->write(RY);

        }

        if(LY > 120 && LY < 135){

        Motor1->write(127);

        }

        if(RY > 120 && RY < 135){

        Motor2->write(127);

        }

      }

       

       

      int main()

      {

        DS4driver myDS4;

        myDS4.init("/dev/input/event2");

        cout << "DS4 Driver test program" << endl;

       

        // loop forever toggling the on board LED every second

        while (1) {

        if( myDS4.update() == 0){ // 0 means error

        break;

        }

        commandMotors(myDS4.leftV, myDS4.rightV);

        floatdelay(0.1);

        }

       

       

      }

       

      when I run the above code there is no compilation error. I have tried debugging the code. I found out that it reads the input from the PS4 analog joystick and returns the digital value using evtest. Because I did the basic debugging technique i.e print something on every if statement, it prints as it should but the values do not command the motor driver. Please can someone tell me what I am doing wrong! The motor driver takes input from 0 to 255 digital values and that commands the direction and speed of the motor attached to it.

       

      Thank you in Advance, I really appreciate your input

        • 1. Re: Eclipse IDE: Digital write
          PabloM_Intel

          Hi dunex_1,

           

          Have you checked the specifications of the motor driver? How much voltage is needed for it to start operating? Also, have you attached direct voltage to the motor driver to confirm that it’s working properly?

          I would also suggest you to check the Galileo and Edison Shield Test report, there are some motor drivers that have been tested and you can use that information.

           

          Regards,

          Pablo

          • 2. Re: Eclipse IDE: Digital write
            dunex_1

            Hi PabloM_Intel,

             

            Thank you for directing me to the path. I have improved my code now but the joystick reading from the PS4 controller is not instantaneous. it takes forever to change when I try to control my motors or even cout some statements for debugging. Can you please look at my code and see if there is any glitch in it.

             

            /*

            *  Example for DS4driver

            */

            #include "mraa.hpp"

            #include <iostream>

            #include <unistd.h>

            #include "DS4driver.h"

             

            using namespace std;

             

            void floatdelay(float delay){

              clock_t start, stop;

              start = clock();

              stop  = clock();

              while(((float)(stop - start)/ CLOCKS_PER_SEC) < delay){

              stop = clock();

              }

            }

             

            double map(double x, double in_min, double in_max, double out_min, double out_max)

            {

              return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

            }

             

            void commandMotors(int LY, int RY){

              mraa::Pwm* Motor1 = new mraa::Pwm(3);

              mraa::Pwm* Motor2 = new mraa::Pwm(5);

              Motor1->enable(true);

              Motor2->enable(true);

             

              float LV;

              float RV;

             

              if((LY <= 120) && (RY <= 120)){

              LV = map(LY, 0, 255, 0.0, 1.0);

              RV = map(RY, 0, 255, 0.0, 1.0);

              cout << "Both up" << endl;

              Motor2->write(RV);

              Motor1->write(LV);

              } else if((LY >= 135) && (RY >= 135)){

              LV = map(LY, 0, 255, 0.0, 1.0);

              RV = map(RY, 0, 255, 0.0, 1.0);

              cout << "Both Down" << endl;

              Motor2->write(RV);

              Motor1->write(LV);

              } else if ((LY <= 120) && (RY >= 135)) {

              LV = map(LY, 0, 255, 0.0, 1.0);

              RV = map(RY, 0, 255, 0.0, 1.0);

              cout <<"Left up and Right down" << endl;

              Motor2->write(RV);

              Motor1->write(LV);

              }else if ((LY >= 135) && (RY <= 120)) {

              LV = map(LY, 0, 255, 0.0, 1.0);

              RV = map(RY, 0, 255, 0.0, 1.0);

              cout << "Left down and Right up" << endl;

              Motor2->write(RV);

              Motor1->write(LV);

              }else if ((LY > 120 && LY < 135) && (RY > 120 && RY < 135)) {

              cout << "Both Stopped" << endl;

              Motor2->write(0.48);

              Motor1->write(0.48);

              }else

              Motor2->write(0.5);

              Motor1->write(0.5);

            }

             

             

            int main()

            {

              DS4driver myDS4;

              myDS4.init("/dev/input/event2");

              cout << "DS4 Driver test program" << endl;

             

              // loop forever toggling the on board LED every second

              while (1) {

              if( myDS4.update() == 0){ // 0 means error

              break;

              }

              commandMotors(myDS4.leftV, myDS4.rightV);

              floatdelay(0.1);

              }

            }

             

            Thank you in advance!

            • 3. Re: Eclipse IDE: Digital write
              DiegoV_Intel

              Hi dunex_1,

               

              I would recommend you to check the PS4 part first, and make sure it works fine. Did you check the value of myDS4.leftV and myDS4.rightV? I would think that those values should indicate that you pressed the left or the right arrow on the PS4 controller, right? What are the values that you expect to get from myDS4.leftV and myDS4.rightV?

               

              Regards,

              Diego

              • 4. Re: Eclipse IDE: Digital write
                dunex_1

                Hi DiegoV_Intel

                 

                Yes I checked the values of myDS4.leftV and myDS4.rightV. And the values indicate that I pressed the two analog stick of a playstation control. They both return a number from 0 to 255 depending on how high or low its pressed. If its not pressed it returns 127 (middle). which is what I mapped in my command motor function. The problem is that the reaction from the controller is not instantenous. It sometimes takes 10 or more seconds to change the speed or output of the motor

                 

                Regards,

                 

                Chinedu

                • 5. Re: Eclipse IDE: Digital write
                  PabloM_Intel

                  Hi dunex_1,

                   

                  It could be the ‘map’ section of the code what it taking too long. If you’re getting your debugging messages and motor response with a lot of delay, I suspect the issue should be the mapping. Have you tried changing the ‘map’ function datatypes? Maybe using integers instead of doubles.

                   

                  Regards,

                  Pablo

                  • 6. Re: Eclipse IDE: Digital write
                    dunex_1

                    Hi PabloM_Intel

                     

                    Since the map function maps the joysticks inputs from between 0 and 255 to between 0.0 and 1.0. If I use int, I will lose data and the mode will always be idle except when its mapped to exactly 1

                    • 7. Re: Eclipse IDE: Digital write
                      PabloM_Intel

                      Hi dunex_1,

                       

                      I would try to change the code so that it consumes less machine resources. As you say, using int exclusively is not an option, but you can try with some other options. Maybe using float variables would be a better idea.

                       

                      Regards,

                      Pablo