Typical YouTube misinformation and the Raspberry Pi. (My fix coming soon.)

Status
Not open for further replies.

()blivion

Active Member
So, I'm cruising YouTube looking at some of the interesting things people have done with their RasPi's, trying to get ideas for my own, and I come across this video and a few others like it...


Looking past the minor sin of bringing a metal tipped object fairly close to bare and live circuitry, right off the gate I'm thinking, "you're doing it way wrong". As can be plainly seen, this person has a Pi, attached to a break out, attached to a bread board, attached to a Arduino, all for what should be the simple and easy to achieve task of moving a servo. That alone wouldn't bother me, I see this kind of unnecessary over complication all the time. And people can do whatever they want with their money + free time. Doesn't hurt me one bit.

The thing that did bothered me though, was that he is selling it as "simple". Granted, he does say multiple times that the alternatives "seem more complicated" as if he is not completely sure his way is the simplest. But still, it baffles me how one even thinks that it is somehow simple to chain thing to thing to thing to thing like that? Especially when the first and last thing are mostly the same thing (an embedded system). His way of doing the task reminds me of those cartoon contraptions where the fan blows the kite that pulls the string that trips the mouse trap that flips the dominos that hits the matchbox car that... you get the idea.

Well, I decided to set out to do it with as little actual "doing" and time as possible (I'm really lazy). I mostly did it because getting the Pi for essentially free from these very forums feels a lot like a small loan of good faith with 10x interest I should pay off to the world. But largely also because I can make use of such things, and it gives me an excuse to play with my newest toy. (^_^)

I don't consider myself good with software at all, and even so I figured out how to do the task with just the Pi in no time. Certainly not as hard to do a task as the video author was expecting. I currently have it working fine with just the Pi, three resistors, and one transistor (and power + servo of course). The code is still unpolished so I am going to work on that a bit more before I post it. But really, when I got my dev setup working (thanks Cboard) it took me realistically like 13~16 hours time on task to do it, and 90% of that was prep work and time spent navigating a search engine just figuring out how to make it work. Now that the code is almost done, it will be copy paste for the rest of the world. With the download, it would take the odd onlooker 30 minutes if he/she can solder and has resistors and transistors already.

Honestly, write one little C CLI app, and build a simple 3-5V transistor buffer. Not that hard. And will be 10x easier when you can just download it from me when it's done too.

(I would upload the video I took a little while ago of it working, but my video recorder is actually a cell phone with the stereotypical embarrassingly bad quality. You're better of with your imagination, more fidelity that way honestly.)
 
Last edited by a moderator:
Oh I'm not judging the guy as a person. Not everyone can be master and commander of the electronics world. I just don't think it's fair to onlookers who find the video and think, "Oh, that says it's really easy" then go off and buy all the gear to find out it's still probably harder. His video has has 23K views, if even 2% are listening intently that's nearly 500 people that are going to be learning to do it the wrong way. I did it for free and used junk drawer parts. And it was fairly simple.

As for the adafruit method, with a quick skim of the link you gave me they seem to use a special part of the chip (the PWM section), and using their own OS. Well I'm using pure software, and doing it on the more common and recommended raspbian distro. And my code can make the servo motor move to any position with reasonable precision. Finally, it can be used for other things. I also have plans to make it do all the pins if possible. Each with their own function/duty cycle.

No promises, but the work is almost done. So OP will most likely deliver on this one.
 
I don't understand the point of writing "generic code modules" for embedded systems so that others could just use it to make their lives easier. It would be more useful to write a tutorial how a servo control works, how pwm works and how to implement pwm in software (and hardware). That way everybody can adapt the information to their specific project and easily write their own code. If people can't code and are too lazy to learn, they should not be doing embedded systems.

EDIT: About the simplicity of the solution: To the guy in the video the solution is simple. All he needed to do is connect couple of wires and maybe write 10 lines of code without having to learn/study anything. The solution is technologically overkill, but so is traveling 100 meters by bicycle, if you start thinking how much work and engineering effort went into building the bike.. and all you did was travel 100 meters. Finding or knowing the simplest solution is many times the hardest thing. You have to study a lot to understand the problem.. then you need to study some tools to solve that problem.. then there is the work itself to find the simplest of all solutions. I agree that the video does not help anyone. He should have kept that particular solution to himself.
 
Last edited:
Welcome to the future.

Harder and harder tasks attempted by stupider and stupider people...

But that's ok, WE will be here to help them with "PLZ make dis fing wrk for me, URGENT I ned to get my degree"...
 
Welcome to the future.

Harder and harder tasks attempted by stupider and stupider people...

But that's ok, WE will be here to help them with "PLZ make dis fing wrk for me, URGENT I ned to get my degree"...

I was excited when I found out that there is an AVR book out called Practical AVR Microcontrollers. "Finally an AVR book that I can recommend to the students" - I thought. Bull***t! The book only teaches how to program using the Arduino IDE. It does not teach anything about the microcontroller itself. The book should be called "Arduino for dummies" and it should not be recommended to anyone.
 
Last edited:
Regarding the Occidentalis OS, I don't think it's required to use the PWM/Servo module; can't the kernel module just be loaded like any other on the raspian OS?

BTW, xxxx "for Dummies" is a closely guarded copyright.
I'm sure we can come up with plenty of substitutes to "for Dummies"
 
i have the same beef about things like using a PIC to generate sine waves, or turn a sine wave into a triangle wave. these are things that can easily be done with analog components and the analog components don't require code. but i see articles all the time in the engineering magazines "generating low distortion sine waves using only one chip", and that chip turns out to be a PIC, which requires two forms of code, a routine to generate the output, and a lengthy lookup table. it's far simpler to use a quad op amp to make a wien bridge oscillator (actually this can be done with a single op amp, but a quad op amp can buffer the output, and create separate square and triangle outputs at the same time), or a state variable filter with positive feedback to make it oscillate, and that also is on a single chip...

don't get me wrong, generating sine waves with a PIC is a good way to do it, but it's not the simplest way, though it might seem so at the time.
 
I agree with you in one way Unclejed, analogue is great and I prefer to reach for one transistor to do a task instead of an IC. And I love simplicity and minimalist design.

But that also goes for reaching for one IC (a PIC) instead of 3 or 5 ICs (analogue)!

I'm one of the people who made sine waves with a PIC and for a very important reason; xtals! A sine from a PIC only needs a couple of parts and some code, and will be xtal locked with very high frequency stability and accuracy. That's not going to happen with your quad opamp and wein bridge circuit. Also the PIC can have buttons to instantly switch between exact Hz settings (10Hz,100Hz,1000Hz etc) again something that costs almost zero parts on the PIC but would be painful in analogue with a rotary switch and multiple calibrations etc.

And as for the "simplest" way, once you already have code is does become the simplest and easiest way... You just re-use the code and change one number to change the frequency. With analogue you can't "re-use" like that, you still have to buy new parts and solder new parts for each new feature added.

Here's a sine/tri/sq generator I did earlier, with exact (xtal locked) Hz steps in the adjustment from buttons;

**broken link removed**
https://www.romanblack.com/onesec/SineDDS.htm

I'd like to see you give that level of functionality "simpler" by using opamps!
 
Hummm... Lots of differing opinions here.

In any case, as promised I have attached to this post the Raspberry Pi v2 PWM control program, and source code that lets one do servo control(among other things) with just the Pi and a dirt simple buffer. I also went the extra mile and wrote a whole lot of safety and explanation into the source for the kids. Know that to simply achieve the underlying fundamental task, I didn't have to do any of that. It was as easy to do as I though it would be.

The precompiled executable file has .bin at the end, take that off when you load it into the Pi and try and run it. I did this just to bypass ETO upload filter, it won't run named like that. So, RENAME the file from PWM_PIN.bin to JUST PWM_PIN when you have it on the Pi. When your done, execute with "sudo ./PWM_PIN" for a rundown on how to use it. I hope this is clear enough (^_^).

Hardware
To use this program with voltages other than the 3.3v of the Pi it's self, you obviously need to have a buffer of some kind on the pin. I chose to use a simple npn common emitter buffer. This is an inverting design, so the off and on time parameters entered into the program need to be switched if you use it. Here is the schematic for such a buffer... (not that it's needed for most of us).




Other notes.
I would also like to stress a few important points.

(1) I'm not actually very good with C, careful examination of the source will expose this fact. So don't trust this program with your life, or attach anything important to the Pi while using it. It could blow up in your face easy, so I take no responsibility for your use of it. Use at your own peril.

(2) The Linux kernel in the recommended raspbian distro is NOT a real-time kernel, so the timing accuracy suffers. In practice, the average accuracy to about 20μS, which is great for a lot of things. But it is quite common to see spikes of up to a few hundred μS too. Finally, it COULD go up to several ms, though I haven't seen this happen. So use this for non timing critical tasks.

(3) My source was built on top of Gert van Loo & Dom's original source, which can be found on the Raspberry Pi's low level peripheral eLinux Wiki page. All rights to the original work fall under their control, not mine. Everything else is... well... whatever GPL license that let's you copy/rewrite/reuse the code, but not copyright it as your own.
 

Attachments

  • PWM_PIN.c
    10.5 KB · Views: 226
  • PWM_PIN.bin
    75.4 KB · Views: 141

So this is useless? Why would you start a thread just to point out that you can't do a better job? .. For the kids.. please. My kids will teach you..

I've been teaching embedded systems and C programming for over five years and your code is the most cryptic PWM/servo control implementation I have ever seen. The comments are arrogant and do not explain what the code does. You claim that "it is more clear in how it works, which is better for those learning", but I can't find any explanation how to use it.. I can't find any simple interface that I can use. All I see is self-explanatory and arrogant comments like "integers for our program" and "Not sure what this is, Don't touch it". I can tell you what that void-pointer is and what it does.. easily.. But now I don't feel like it.. I'll let you figure it out.
 
Last edited:
You certainly don't hold your punches, do you

But can you explain why there is a need for both the global variables, "void *gpio_map" and "volatile unsigned* gpio"?
 
You certainly don't hold your punches, do you

Yeah.. I should apologize to ()blivion.. I was in a bad mood there. Sorry.

But can you explain why there is a need for both the global variables, "void *gpio_map" and "volatile unsigned* gpio"?

I can explain how they are used. The "void *gpio_map" is practically useless.. It is used as a temporary variable inside the "setup" function. I don't know why the code is written that way.. strange.
 
Last edited:
Hardware
To use this program with voltages other than the 3.3v of the Pi it's self, you obviously need to have a buffer of some kind on the pin. I chose to use a simple npn common emitter buffer. ...

Most servos (pretty much ALL servos) will work fine with a control signal of 3.3v or 3v amplitude. Their input stage only needs about 2v to trigger and register an input pulse. So the transistor buffer might not be needed.
 
Ok, so in summary...
1) the original video is fine,
2) the "improved" rPi code has issues with unreliable timing (which will generally always be the case if relying on software for the timing), and
3) Mr RB made a sinewave oscillator.
 
WOW! Was not expecting misterT to blow up there. But apologies were given, and I accept, so that is a non issue to me. But I will defend my work non the less.



dougy83 said:
But can you explain why there is a need for both the global variables, "void *gpio_map" and "volatile unsigned* gpio"?
Thanks dougy83, that was in fact the source of my confusion. I am generally aware of what a void pointer is otherwise. Also keep in mind that this was not my doing. It can be seen in the original C code. I myself can't explain why they did it that way, but I imagine that had a good reason.

Mr RB said:
Most servos (pretty much ALL servos) will work fine with a control signal of 3.3v or 3v amplitude. Their input stage only needs about 2v to trigger and register an input pulse. So the transistor buffer might not be needed.
That's good to know. That makes it even simpler. I just figure 5V servo, so 5V input. And of course, the circuit is intended to cover a whole range of other basic PWM projects, not just servos. But truly, servos were the primary goal.

1) the original video is fine,
Again, just to be clear, I'm not judging the guy for doing things his way. I'm just saying there is another way that better fits the description of "simple". Which is in direct contrast to what he is claiming in the video. But, of course, he can and will do things exactly the way he wants to. I have no problem with that. I just think people deserve an alternative. And my way fits that goal.

dougy83 said:
2) the "improved" rPi code has issues with unreliable timing (which will generally always be the case if relying on software for the timing),
Yes, there are probable timing issues to be sure, but the worst I have seen in actual practice is some fast jitter in the servo arm, almost a vibration. The worst timing I have got out of it was a pulse or two of ~750 μS once or twice after half an hour of trying, and this was when I was deliberately loading the system with other tasks to try and screw up the timing with context switching. The specs do say there could be up to several ms of delay, but realistically the timing is accurate down to ~20μS for about 99.999% of the time provided your not TRYING to cause the system grief. In any case, it is more than enough to control servos accurately.

misterT said:
So this is useless? Why would you start a thread just to point out that you can't do a better job?
I did do a better job. The program, (at least the compiled application), is FAR better and FAR from useless. It does exactly what I set out to do, and then some. And it is a better way of doing it than what's in the video. The biggest issue is timing, but servos don't need super precision accurate timings. They have lots of gear and linkage slop and are quite forgiving when it comes to a few really messed up timing pulses. There is no issue with my code when it is used correctly. I was saying there COULD be issues and bugs I am unaware of. It's beta code after all.

With respect, I think you are mistaking all the extra interface and error catching code as my way of doing the actual PWM, this is not the case. The actual PWM code is in the function DO_GPIO() near the bottom, it is four lines in a loop of the form "set(pin) delay(x) clear(pin) delay(y)" hardly cryptic. Though, I admit, the rest is certainly daunting, it's an unfortunately necessity on the given platform.

Don't forget, this is not your typical 8bit PIC or AVR. It's a complex multi-user Linux OS on a media SoC of the Von-neumann architecture. It's also vast, it has several hundred megabytes of RAM, and ROM in the multi-gigabytes range and several thousand packages installed on it with several dozen process running at any given time. Finally, none of it was ever intended to do real-time tasks.

So... one can't throw in a just a simple PWM loop and call it a day. The thing would almost certainly implode if you did that. I had to improvise heavily to keep things safe and stable. There has to be a bunch of crap on top of the PWM code just to deal with the OS. A Von-neumann architecture also means that some RAM addresses are actually program code, which would be detrimental if overwritten. Then, you have to figure that there are many critical RAM addresses that if you write to may literally fry your Pi. So, I had to go through great pains just to make sure that when a person typos when invoking the program, the program doesn't use that as arguments for the code that runs with kernel level privileges operating on RAM. Problems like that would be a Greek tragedy for any Pi owner.

The point about me not being good at C was more "I'm not great at C... when compared to the gurus that spend their lives specializing in it" This was just a humble and fair warning that there could easily be bugs in the code. I don't want people hooking my work up to granny's respirator or some industrial machinery. This is beta software, hot of the press, and I'm not perfect, there are bound to be mistakes. So you use this software at your own risk. Simple as that.

My comments were arrogant? There was really nothing arrogant in the source code, I can only barely fathom why you would see it this way. "I don't know what this is, don't touch it" was also a fair warning to stay away from things not understood. Playing with such things could break the Pi. Even if such comments make me look arrogant or incompetent, I'm not going to leave a loose end like that just lying around. If I don't know, it's better I just say "don't touch this" so people... don't touch it. Also, some of the comments, and a lot of the error messages, were targeted at myself, not others. I talk to myself second person a lot. With that in mind, how can one be arrogant toward oneself?

Finally, for an explanation to how my program is used. As I said, invoke the program on the Pi with no (or bad) arguments. (sudo ./PWM_PIN) Just like any other CLI program, it will spit out an intuitive error, and/or remind you how to use it. I did this so I don't have to make a read me, and so it only spits out what you need to know about the mistake you just made. I don't feel a person should have to read through a readme or instructional unless they are actually having problems.

I hope this clears up all the misunderstandings.
 

Worst thing that happens when somebody touches that variable is that the code won't compile.. no harm done.. Pi did not implode.. "I don't know what this is, don't touch it" is not a fair warning. It is north-korea style propaganda. You should explain what each variable is and smart people will understand not to touch it if they do not know how it is used. Stupid people are stupid and there is no way to prevent stupid people from doing stupid things. Write for the smart and ignore the stupid. I can see you can code etc. but don't write "for kids" or "for the stupid people".. You should write for the smart people.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…