fivdi / pigpio

Fast GPIO, PWM, servo control, state change notification and interrupt handling with Node.js on the Raspberry Pi
MIT License
949 stars 89 forks source link

motor.servoWrite(0) not working #102

Closed Janaka-Steph closed 4 years ago

Janaka-Steph commented 4 years ago

Hello,

I would like my Tower Pro Micro Servo 9g to stop applying force to keep the position after the action is done. I tried motor.servoWrite(0), motor.pullUpDown(Gpio.PUD_OFF), motor.pullUpDown(Gpio.PUD_DOWN), motor.digitalWrite(0) but nothing works. I don't see what I am doing wrong.

import {Gpio} from 'pigpio'

const motor1 = new Gpio(26, {mode: Gpio.OUTPUT})
const motor2 = new Gpio(19, {mode: Gpio.OUTPUT})

const closePosition = 500
const openPosition = 2500
const increment = 100

const getMotor = (pin) => {
  switch (pin) {
    case 1: {
      return motor1
    }
    case 2: {
      return motor2
    }
    default: {
      console.log('An incorrect servo pin has been provided')
      return null
    }
  }
}

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

async function activateServo(pin) {
  let pulseWidth = closePosition
  while (pulseWidth !== openPosition + increment) {
    getMotor(pin).servoWrite(pulseWidth)
    await sleep(50)
    pulseWidth += increment
  }
  getMotor(pin).servoWrite(0)
}
activateServo(1)
fivdi commented 4 years ago

Calling motor.servoWrite(0) should do it.

The program posted above is not a complete program and contains errors. For example, the following line contains a '?'

  getMotor(pin)?.servoWrite(0)

Please post a complete and minimal program that can be used to reproduce the error.

Janaka-Steph commented 4 years ago

I have edited the example above. This is basically the code I have but with more servos. The "?" is Typescript, I removed it.

fivdi commented 4 years ago

Did you run the program posted above to verify that it reproduces the error? I don't think so as increment isn't defined.

It's important that we don't waste each others time. Please post a complete and minimal program that can be used to reproduce the error. Please verify that the program posted can be used to reproduce the error. I need the exact code.

Janaka-Steph commented 4 years ago

You are right, I am sorry. I have updated the example and tested it. It starts at 500 and goes to 2500, then it is supposed to turn it off but does not.

fivdi commented 4 years ago

The program calls servoWrite with 500 (most anti-clockwise) to 2500 (most clockwise) in steps of 100, then calls servoWrite(0) and then terminates. As the program terminates, it can't be the program that is holding the servo in position. The program can't generate pulses to hold the servo in position after it terminates.

What I think is that you haven't calibrated the servo to find out what the min and max values are that can be passed to servoWrite. I would imagine that passing the value 2500 to servoWrite has forced the servo so far clockwise that it's hard to move the shaft manually and that a lot of pressure needs to be applied to achieve this.

Change this code

const closePosition = 500
const openPosition = 2500

to this

const closePosition = 1000
const openPosition = 2000

to see if it's easier to move the servo shaft after the program terminates.

The min and max values that can be passed to servoWrite are a different for each servo so calibration is needed for each servo.

Janaka-Steph commented 4 years ago

Hi! I tried your suggestion, moving between 1000-2000, even 1300-1700, but I have the same issue. I will buy a different more expensive servo, maybe those cheap Tower Pro SG90 are defectives. Maybe having external power supply would change something? Also my machine have five servos connected to a RPI 3B, I don't know if it is actually a real issue if all five hold position, maybe the board can handle it.

Janaka-Steph commented 4 years ago

Something I didn't mention but may be relevant is that even when I stop my node app with Ctrl+C the servo is not released. Switching off the Pi with sudo shutdown now don't release the servo either. I have to unplug the power supply. This is really weird.

fivdi commented 4 years ago

Shutting down the Raspberry Pi doesn't remove the power from the power pins on the GPIO header.

Janaka-Steph commented 4 years ago

I solved my problem. The issue was the servos Tower Pro SG90 I used. I tried with a Tower Pro MG90S and it works like a charm. Thank you for your help!