Code Monkey home page Code Monkey logo

Comments (8)

alcalyn avatar alcalyn commented on May 30, 2024

Well, I just found the examples/ dir at the root... I'll check out :)

from phpi.

calcinai avatar calcinai commented on May 30, 2024

Nice work. If anything needs clarification, let me know

from phpi.

alcalyn avatar alcalyn commented on May 30, 2024

Yep, thank you, I'm actually trying this library.

from phpi.

alcalyn avatar alcalyn commented on May 30, 2024

Well, I'm experiencing some issues.

I have this script:

include __DIR__.'/vendor/autoload.php';

use Calcinai\PHPi\Pin\PinFunction;

$loop = \React\EventLoop\Factory::create();
$board = \Calcinai\PHPi\Factory::create($loop);

$board->getPin(12)->setFunction(PinFunction::PWM0);

$pwm0 = $board->getPWM(\Calcinai\PHPi\Peripheral\PWM::PWM0)
    ->setRange(512)
    ->setEnableMS(true)
    ->start();

$dc = intval($argv[1]);
var_dump($dc);

$pwm0->setDutyCycle($dc);

$loop->run();

Then I run it with sudo php pwm.php 50, where 50 is the duty cycle I set from 0 to 100. The effect is that my robot moves as I want, but I could not vary motor speed.

  • First, using sudo php pwm.php 50 seems to set the duty cycle at 100%. So I tried to change range with ->setRange(256), and run it, but it does nothing, 50 is still setting 100% dc.

Btw I can do with wiringPi in command line: gpio pwm 26 350, then it works, and 512 seems to be the 100% duty cycle value.

  • Also, I don't find how to use software PWM, for example if I want to run PWM in an other pin, not handled by hardware PWM.

  • Finally, I have seen in some forum that I could use MSMODE for PWM, so can I use ->setEnableMS(true) to do the trick ?

from phpi.

calcinai avatar calcinai commented on May 30, 2024

I can't see any reason that what you've got won't work. Out of curiosity, have you tried omitting ->setRange() and -> setEnableMS()?

I had implemented the soft pwm, but it was quite CPU heavy. I'll dig out the example code for that in a sec.

I don't have my test hardware on me at the moment, but I can have a look this afternoon if you can hold on!

from phpi.

alcalyn avatar alcalyn commented on May 30, 2024

Yes I started my tests without setRange and setEnableMS.

I don't find any difference with setEnableMS, just tried.

And I tried setRange(512) and setRange(256) to try to fix the setDutyCycle(50) case, and because I've seen the default one in the library is 1024. But setDutyCycle(50) always results in a 100% dc.

I don't need a high frequency PWM (100hz is suffisant as I tried it with python), so I guess software PWM could also be suffisant with PHP.

But in fact I don't really need software PWM for now, I can do without.

from phpi.

calcinai avatar calcinai commented on May 30, 2024

@alcalyn - sorry it's taken me so long to get back to you on this one. I've just tested equivalent code:

<?php

include __DIR__.'/vendor/autoload.php';

use Calcinai\PHPi\Peripheral\PWM;
use Calcinai\PHPi\Pin\PinFunction;

$board = \Calcinai\PHPi\Factory::create();

$board->getPin(12)->setFunction(PinFunction::PWM0);

$pwm0 = $board->getPWM(PWM::PWM0)
    ->setRange(512)
    ->setEnableMS(true)
    ->start();

$dc = intval($argv[1]);
var_dump($dc);

$pwm0->setDutyCycle($dc);

And it works as expected. Looking at the range function, it appears to me that that only sets the resolution in the steps - i.e. the setDutyCycle() will still accept 0-100, but will only respect a resolution specified by the range.

The MS function is supported and working in my case too. Are you sure that the motor driver supports the switching speed? What happens if you connect an LED to BCM 12?

from phpi.

alcalyn avatar alcalyn commented on May 30, 2024

Hi, you're right, I tried your snippet replacing dc motor with a LED, it works as expected !

Also, with your code snippet, setting a duty cycle at 7 or less results as no rotation (but the led is a little on), and setting it from 8 to 100 results as full power (whereas the led light on as expected).

So I thought that my DC motor can't handle PWM, but I think that it's only because the clock has a too high frequency. I think that because I made it work with a software PWM on python (which means low frequency PWM). Then I tried a "software" PWM with:

while (true) {
    $pwm0->setDutyCycle(100);
    usleep($dc - 10);
    $pwm0->setDutyCycle(0);
    usleep((100 - $dc) - 10);
}

And I could get some interesting result. But I'm limited because of this line:

usleep(10);

That's why I added - 10 in my usleeps.

Then I also tried to change usleep from this library from 10 to 1, and set a duty cycle at 1, which worked a little better.

So I think that either a software PWM or $pwm->setFrequency could do the trick. (But I couldn't find how to use setFrequency).

By the way, PWM works so I close the issue.

from phpi.

Related Issues (11)

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.