controlling more than one servo with the AdvancedServo board

Supporting Browser-based Javascript and Node.js
jojoba
Fresh meat
Posts: 2
Joined: Wed Feb 13, 2019 5:22 pm
Contact:

controlling more than one servo with the AdvancedServo board

Postby jojoba » Wed Feb 13, 2019 5:44 pm

Greetings!

I love Phidgets!

I am attempting to control more than one servo via the AdvancedServoControl 8-motor board and am close, but coming up short. I've pasted my code below. Do you happen to have any guesses as to why I can't control more than one servo? Thank you kindly.

Best,
matthew

Code: Select all

        var phidget22 = require('phidget22');
        var serverConnection = new phidget22.Connection(
            this.SERVER_PORT,
            this.PHIDGETS_SERVER_HOSTNAME,
            {
                name: 'Server Connection',
                passwd: ''
            }
        );


        serverConnection.connect().then(() => {
            console.log('connected to phidgets server');


            this.rcServo_0 = new phidget22.RCServo();

            this.rcServo_0.onAttach = ch => {
                console.log(ch + ' attached');
                this.rcServo_0.setChannel(0);
                this.rcServo_0.setTargetPosition(90);
                this.rcServo_0.setEngaged(1);
            };
       
            this.rcServo_0.open().then((ch) => {
                console.log('channel open');
                //console.log(this.rcServo_0);
            }).catch(function (err) {
                console.log('failed to open the channel:' + err);
            });


            this.rcServo_1 = new phidget22.RCServo();

            this.rcServo_1.onAttach = ch => {
                console.log(ch + ' attached');
                this.rcServo_1.setChannel(1);
                this.rcServo_1.setTargetPosition(90);
                this.rcServo_1.setEngaged(1);
            };
       
            this.rcServo_1.open().then((ch) => {
                console.log('channel open');
                //console.log(this.rcServo_0);
            }).catch(function (err) {
                console.log('failed to open the channel:' + err);
            });


        });

jdecoux
Labview Developer
Posts: 84
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: controlling more than one servo with the AdvancedServo board

Postby jdecoux » Thu Feb 14, 2019 11:10 am

You're close, but you need to move the addressing of your channels from your attach handlers to instead happen before opening them. It is needed to decide which channels to attach.

In this case, that means moving setChannel() before the open() call for each.

Code: Select all

var phidget22 = require('phidget22');
var serverConnection = new phidget22.Connection(
   this.SERVER_PORT,
   this.PHIDGETS_SERVER_HOSTNAME,
   {
      name: 'Server Connection',
      passwd: ''
   }
);


serverConnection.connect().then(() => {
   console.log('connected to phidgets server');


   this.rcServo_0 = new phidget22.RCServo();

   this.rcServo_0.setChannel(0);
   
   this.rcServo_0.onAttach = ch => {
      console.log(ch + ' ' + ch.getChannel() + ' attached');
      
      this.rcServo_0.setTargetPosition(90);
      this.rcServo_0.setEngaged(1);
   };

   this.rcServo_0.open().then((ch) => {
      console.log('channel open');
      //console.log(this.rcServo_0);
   }).catch(function (err) {
      console.log('failed to open the channel:' + err);
   });


   this.rcServo_1 = new phidget22.RCServo();

   this.rcServo_1.setChannel(1);
   
   this.rcServo_1.onAttach = ch => {
      console.log(ch + ' ' + ch.getChannel() + ' attached');
      
      this.rcServo_1.setTargetPosition(90);
      this.rcServo_1.setEngaged(1);
   };

   this.rcServo_1.open().then((ch) => {
      console.log('channel open');
      //console.log(this.rcServo_0);
   }).catch(function (err) {
      console.log('failed to open the channel:' + err);
   });


});


Return to “Javascript”

Who is online

Users browsing this forum: No registered users and 1 guest