Git Product home page Git Product logo

Comments (8)

shanghuiyang avatar shanghuiyang commented on July 27, 2024 2

this is my golang codes using pwm to control motor, FYI.
https://github.com/shanghuiyang/rpi-devices/blob/master/app/car/car.go

from go-rpio.

drahoslove avatar drahoslove commented on July 27, 2024

Hi,
in PWM mode the freq method does not set the output frequency, but the base clock frequency of the channel. You have to multiply it by the cycle length (because the output frequency is given by base clock frequency divided by cycle length as it is written in the doc).
This should work:

	pin.Mode(rpio.Pwm)
	pin.Freq(50 * 50)
	pin.DutyCycle(0, 50)
...
        pin.DutyCycle(7, 50)

or this:

	pin.Mode(rpio.Pwm)
	pin.Freq(50 * 100)
	pin.DutyCycle(0, 100)
...
        pin.DutyCycle(15, 100) // this will give you the 7.5/50 ratio you have in your python code

from go-rpio.

AndrewKovalenko avatar AndrewKovalenko commented on July 27, 2024

I have similar issue. I try to control electric motor speed with hardware PWM.
Just to test that go-rpio is working I created a simple example which suppose to make motor spinning at ~75% of max RPM:

	if err := gpio.Open(); err != nil {
		log.Printf("Error opening GPIO: %s", err.Error())
		return err
	}
	defer func() {
		gpio.StopPwm()
		gpio.Close()
	}()

	pin := gpio.Pin(18)
	pin.Pwm()
	pin.DutyCycle(3, 4)
	pin.Freq(5000 * 4) // 5000Hz * 4 cycle length
	gpio.StartPwm()

	time.Sleep(10 * time.Second)

Running this code has zero effect.
Could you pls help me to troubleshoot it?

from go-rpio.

drahoslove avatar drahoslove commented on July 27, 2024

@AndrewKovalenko

  • What Raspberry pi generation you have? There might be some incompatibility witch the newest version.
  • What number is your pin? Only some pins support the PWM mode.
  • Are you sure that 5000Hz s what your motor needs to operate? (edited)
  • Are you running the code as root? PWM modes require that.

from go-rpio.

AndrewKovalenko avatar AndrewKovalenko commented on July 27, 2024

@drahoslove thank you for quick response.

  • I use Raspberry Pi 4 Model B 2 GB
  • As you can see form my example - I use pin 18 which suppose to support hardware PWM. I checked the wiring and it is correct. I see voltage change if I set this pin to output and high
  • As for freq - it would be easier for me to run pwm signal at lower frequencies but I found a comment in go-rpio code saying Param freq should be in range 4688Hz - 19.2MHz to prevent unexpected behavior so I set frequency to 5kHz to stay within recommended range. My motor should work fine on those frequencies.
  • I tried both root and non-root. I found that if I just set pin to high with pin.High() function call - it doesn't work when I run my app with sudo but works fine without it.
    For PWM - I don't see any signal coming out either way: with or without sudo

from go-rpio.

drahoslove avatar drahoslove commented on July 27, 2024

@AndrewKovalenko
The RPi 4 model is not fully supported, but I merged the pull-request #50 which might solve your issue. Please try it.

from go-rpio.

AndrewKovalenko avatar AndrewKovalenko commented on July 27, 2024

@drahoslove thank you. I just tried to pull the latest - it seems like I still have an issue.

To exclude the problem with the motor I connected an LED instead of motor and control it from pin 18 via 2n222 transistor.

Here is a listing of my "testing app":

package main

import (
	"log"
	"time"

	gpio "github.com/stianeikeland/go-rpio/v4"
)

const cycleLength = 100
const pmwClockFrequency = 50 * cycleLength // 50kHz

func main() {
	if err := gpio.Open(); err != nil {
		log.Fatalf("Error opening GPIO: %s", err.Error())
	}

	defer func() {
		log.Println("Stop PWM")
		gpio.StopPwm()
		gpio.Close()
	}()

	pin := gpio.Pin(18)

	pin.Output()

	for i := 0; i < 10; i++ {
		log.Printf("Toggle pin. i=%d \n", i)
		pin.Toggle()
		time.Sleep(time.Second)
	}

	pin.Pwm()

	pin.Freq(pmwClockFrequency)
	log.Println("Start PWM")
	gpio.StartPwm()

	log.Println("10%")
	pin.DutyCycle(10, cycleLength)
	time.Sleep(3 * time.Second)

	log.Println("30%")
	pin.DutyCycle(30, cycleLength)
	time.Sleep(3 * time.Second)

	log.Println("50%")
	pin.DutyCycle(50, cycleLength)
	time.Sleep(3 * time.Second)

	log.Println("70%")
	pin.DutyCycle(70, cycleLength)
	time.Sleep(3 * time.Second)

	log.Println("100%")
	pin.DutyCycle(100, cycleLength)
	time.Sleep(10 * time.Second)
}

The interesting issue I observe is:
when I run this app for the first time - LED is blinking while the pin is in output mode and then nothing happens in PWM mode. However if I run the same app second time - LED doesn't blink any more. It just turns on for 10 sec and goes off once pin is set to PWM. This issue persists until I reboot RasPi.

from go-rpio.

rami-dabain avatar rami-dabain commented on July 27, 2024

related #68

from go-rpio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.