Skip to main content

setting up pwm

There's always an obstacle..

I implemented the pwm module as described in the link in the previous post, but oddly enough, it didn't work.

The error I received:

AttributeError: 'module' object has no attribute 'PWM'

After some time scratching my head, I came to the realization that somehow my python was running an older version of the RPi.GPIO library, as PWM control was a pretty recent addition to the library. 

And indeed it was. 

Not really sure what's happening in detail, but it seems that there are two different... entities? that manage libraries for Python. One is through the usual "apt-get" command and the other is "pip." It appears that Python looks at "pip" first, and it currently linked RPi.GPIO to a really old version of the library. 

Uninstalling that older version ("pip uninstall RPi.GPIO") and using the version I obtained through apt-get ("apt-get install python-rpi.gpio") solved this. 

Now, a less-hacky implementation of PWM. 

Note that the PWM offered in this library is a software-driven PWM, meaning that the cycles it counts are based on the cycle count of the process (program). This can be inaccurate because programs can be put "on hold" by the operating system during heavy load, and during this time on hold, it stops counting. Because PWM relies on timing, this could cause "jitters" in the devices that read the signal. A better implementation is a hardware-driven PWM, which uses the clock cycles in the physical board to count. 

For now, I think the software-driven PWM will be fine.

Time's up for working on this today; I'll change messages into JSON next time, I guess.

Comments

Popular posts from this blog

finally getting back to it

Things went by pretty quickly this month, and I was not able to play around with this project much.  I did tinker with it every now and then, but I was never able to get myself past the residing issue of compiling the RF library in the Raspberry Pi. It seemed people are generally interested in the project linked in the previous post, as people were actively posting comments on it.  Couple of days ago a user posted a solution to the compile issues (changes to method names and usage of pre-defined values for parameters), and I thought I should try following it to see if it works.  The code indeed compiled, and I was able to change it a little more to be more fitting to my project.  On the Arduino side, it might be a premature assumption, but it seemed like the Arduino was unable to handle messaging through the RF and controlling the servo at the same time. My assumption is that the inability to control them in separate threads is causing some timing issues, but I mig

finally got around to it (nrf24l0+ and servo)

On a previous post , I used the nrf24l01+ wireless chip to communicate between the Raspberry Pi and an Arduino, but only got lights to turn on. I remember being confused as to why servos would not work, and somewhat left it there. I started messing around with it again, and I am concluding that it might have been just a power issue. Here is the servo moving properly: The Arduino is on the ground due to the short length of the wires powering them. Just as a recap, what is happening is: - a C++ program using the RF24 library is compiled in the Raspberry Pi (connected to an nrf24l01+ chip) to broadcast a message. When executed, it will broadcast the message. - the Arduino (connected to another nrf24l01+ chip) programmed to receive messages receives the message, and upon receipt sends a signal to an Arduino that is wired to the servo to move the servo. Two separate Arduinos are used, as it seems that the servo library and the RF24 library do not seem to run properly toget

GPIO testing

Since the environment of the client-server-target is currently quite functional in the messaging standpoint, I decided to try connecting a simple LED light to the Raspberry Pi to see if it could "open" a door when the command is received. Installed the RPi.GPIO module (a Python module that gives the user command of the GPIO--General-Purpose Input/Output--ports in the Raspberry Pi) and connected the LED light to a GPIO pin. When the target received a request message, it would signal the GPIO pin to drive a current to the LED to light it up for 2 seconds. in main: if __name__ == "__main__":   GPIO.setmode(GPIO.BCM)   GPIO.setup(17, GPIO.OUT) in the on_message's request-received logic:      GPIO.output(17, True)      time.sleep(2)      GPIO.output(17, False) The video is a weak visual result. You can a click when the LED lights up; that was the sound of the key being pressed that sent the request message. Now, time to get (think up of ) some a