2 Replies Latest reply on Jan 26, 2015 3:02 AM by AnandSatya

    Cannot control Servo using "jsupm_servo" module

    AnandSatya

      Hi, I am trying to control servo output using a potentiometer. Code is written using Intel XDK. Below code is not working.

       

      Code:

      var mraa = require('mraa'); //require mraa

      console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console

       

      //Load servo module.

      var servoModule = require("jsupm_servo");

      //Instantiate Servo module on digital port 6

      var servo = new servoModule.Servo(6);

      servo.setMinPulseWidth(544);

      servo.setMaxPulseWidth(2400);

       

      var analogPin0 = new mraa.Aio(0); //setup access analog input Analog pin #0 (A0) i.i. output of potentiometer

       

      moveServo();

       

      function moveServo()

      {

          var analogValue = analogPin0.read(); //read the value of the analog pin

          analogValue = Math.floor(analogValue/2); //potentiometer returns intergers between 0 to ~500

          console.log(analogValue); //write the value of the analog pin to the console

          //servo.setAngle(analogValue);

         

          servo.setAngle(analogValue);

          console.log(analogValue);

          sleep(100, function() {

              // executes after one second, and blocks the thread

          });

          setTimeout(moveServo,0);

      }

       

      function sleep(time, callback) {

          var stop = new Date().getTime();

          while(new Date().getTime() < stop + time) {

              ;

          }

          callback();

      }

        • 1. Re: Cannot control Servo using "jsupm_servo" module
          AnandSatya

          Guys i found the issue. "jsupm_servo" is setting incorrect time perios to PWM signal. Required is 20ms but it is seeting 7.9ms.

           

            

          GPIO FileArduinoXDK
          /sys/class/pwm/pwmchip0/pwm1/period190000007968000
          /sys/class/pwm/pwmchip0/pwm1/duty_cycle
          0 degrees544000544000
          90 degress14410001472000
          Fix: Need to modify XDK's "jsupm_servo"  library to correct PWM period

           

          Where should i file this bug?

          • 2. Re: Cannot control Servo using "jsupm_servo" module
            AnandSatya

            As a workaround you can use the following code to control your servo:

             

            Code:

            var AIO_pin = 0;//setup access analog input Analog pin #0 (A0) i.e. output of potentiometer. Assuming values read is between 0 and 1000.

            var Servo_pin = 5;//Initialize PWM on Digital Pin #5 (D5) and enable the pwm pin

            var PWM_period_us = 20000;

            var Min_Duty_Cycle = 0.029;

            var Max_Duty_Cycle = 0.087;

             

            var mraa = require("mraa"); //require mraa

            console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console

            var analogPin0 = new mraa.Aio(AIO_pin);

            var pwm = new mraa.Pwm(Servo_pin);

            pwm.enable(false);

            pwm.period_us(PWM_period_us);

             

            pwm.enable(true);

            moveServo();

             

            function moveServo() {

                var analogValue = analogPin0.read(); //read the value of the analog pin

                var processedValue = MapRange(analogValue,0,1000,Min_Duty_Cycle,Max_Duty_Cycle);

                pwm.write(processedValue); //Write duty cycle value.

                console.log(analogValue+" "+processedValue+pwm.read());

                setTimeout(moveServo,200);

            }

             

            function MapRange (in_vaule, in_min, in_max, out_min, out_max) {

                var output = (in_vaule - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

                if (output >= out_max) {

                    output = out_max;

                } else {

                    if (output <= out_min) {

                        output = out_min;

                    }

                }          

                return output

            }