Arduino Forum

Community => Bar Sport => Topic started by: yendis on Jan 05, 2016, 07:04 pm

Title: A Eureka Moment!
Post by: yendis on Jan 05, 2016, 07:04 pm
I don't know if this is an appropriate post for this section, but I just wanted to share some joy.  :)

My project is to control the speed of a DC PM motor for a classic car project.  The car will have a non-standard gearbox which has no mechanical speedometer drive.  Since I want to keep the original (mechanical) speedometer then this has to be driven by a motor, from electrical pick up pulses related to road speed. 

As a newbie to the Arduino, I have been on a steep learning curve this year.  Starting in April 2015, I decided to learn 'C' as this is the basis of the Arduino sketch language.   Then having bought an Uno, and built up an interface board, I began to put some code together.   I decided not to use the PID library as I wanted to learn the hard way - and assumed that my own code would be easier to understand (in hindsight I am not sure that it was!).

When first connected up, after a bit of debugging the motor did run, and follow the demand signal to a fashion, but there were all sorts of problems.  The speed was unstable, erratic, and sometimes the motor either jumped to full speed, or just stopped and sulked.   

Many weeks passed by as I tried to tune the P and I constants to get it under control, but I seemed to swap one problem for another and felt like I wasn't getting there.  But whilst doing this, I did make a lot of changes to the way I was declaring variable types in order to eliminate errors and overcome limitations of the code.   

Last week I posted that I still had instability problems, especially at low speed start up, and I was thinking of going back to the drawing board, and trying a brush-less motor drive instead. 

But after some further changes to variable types, and more tweaking of the P and I constants, today I finally cracked it!  The motor now tracks road speed (from a pulse generator) closely from 5 mph right up to max speed quite closely, and any overshoot is short lived.

The moral of my story?  Well I guess patience and perseverance, and watch out for data typing problems.

Whilst I am far from expert, I hope that now I will be able to help someone else at least in some small way on a similar project.

Title: Re: A Eureka Moment!
Post by: raschemmel on Jan 05, 2016, 07:13 pm
While this is all well and good, the purpose of the forum is to provide a database of useful information. Unless you intend to post a complete project package with schematics, code and photos,
your post is of no value to anyone. We already know that patience and perseverance are virtues, thank you very much. Thanks for the heads up anyway. If you really want to help others, post your project in the Exhibition Gallery with photos, and code. Since your an EE, you should have a lot to offer.
Title: Re: A Eureka Moment!
Post by: keeper63 on Jan 05, 2016, 07:56 pm
Minor nitpick, but something that needs to be corrected: There is no such thing as an "Arduino sketch language"; Arduino sketches are written in C/C++, nothing more, nothing less. Behind the scenes, a pre-compiler does some "magic" and adds in the main() function (which calls setup(), then repeatedly calls loop() in an endless looping construct), plus the Arduino library headers - and a few other minor things.

All of this is done to help beginners feel more comfortable with using the system, as well as to improve development time; even so - a simple template "starting point" sketch could have been included and used, instead of the hidden "magic" - but then that would complicate the teaching of the system for newbies, as you would then have to explain what all the other parts were, first, before you could even get to the coding portion. It's debatable which is better, but it is what it is.

Furthermore, the term "sketch" is nothing more than a "marketing term" - one could call it instead "a program" or "source code" or something similar, and it would mean virtually the same thing. The term "sketch" instead is a name to make the system more approachable for newbies to programming.

Most of all of this, by the way, is historical re-purposing of the Wiring and Processing projects (both open source, IIRC), which I believe both used similar (or same) terminology, with the same goal of making software development and hardware embedding more approachable. However, the boards that were used back then, while superficially similar to the Arduino, were somewhat of a different nature and purpose - and were used as the inspiration to create the Arduino, instead, while parts of those systems (like the similarities with the Processing language constructs, as well as the IDE which came from Wiring) were borrowed.

At least, that's how I understand things; if I am wrong in any of this - please correct me.

:)
Title: Re: A Eureka Moment!
Post by: raschemmel on Jan 05, 2016, 08:02 pm
Quote
Furthermore, the term "sketch" is nothing more than a "marketing term"   
One I never cared for , because it makes the program sound like a casual collection of jots on a pad,
when, in reality, it could be very complex.
Title: Re: A Eureka Moment!
Post by: yendis on Jan 05, 2016, 09:02 pm
OK if I am wrong about the 'Arduino sketch language', please explain.

Much of the source code which we use to program the Arduino is not in any C reference I have seen. I taught myself C before beginning to use the Arduino, but still had to learn a lot of new stuff.

Confused of Surrey.


Title: Re: A Eureka Moment!
Post by: liuzengqiang on Jan 05, 2016, 11:32 pm
Just wondering what values you set your D to. Most of the time, a non-zero D leads to jittery responses since it draws from the change of values over short period of time. I usually only use P and I and only use D if the single is relatively clean and I need a faster response.

Arduino IDE is using C/C++ language so you can say that you learned C/C++ by using Arduino. I think Processing calls its program code sketch, which makes a lot of sense. It is used to visualize things so it is like a drawing or a sketch. Arduino/Wiring probably borrowed the term and claim to have used it for marketing purpose to make it sound less hard than code or program.
Title: Re: A Eureka Moment!
Post by: raschemmel on Jan 06, 2016, 12:57 am
Quote
Arduino/Wiring probably borrowed the term and claim to have used it for marketing purpose to make it sound less hard than code or program.   
Well they fooled me. I started with arduino thinking I study C++ later and next thing you know I'm learning C++...
Title: Re: A Eureka Moment!
Post by: Henry_Best on Jan 06, 2016, 04:32 am
Furthermore, the term "sketch" is nothing more than a "marketing term" - one could call it instead "a program" or "source code" or something similar, and it would mean virtually the same thing. The term "sketch" instead is a name to make the system more approachable for newbies to programming.
The Arduino Cookbook calls them 'recipes'.
That's not to everyone's taste, though.
Title: Re: A Eureka Moment!
Post by: larryd on Jan 06, 2016, 05:35 am
The use of non programming terms makes the platform more acceptable to people in the arts.
Title: Re: A Eureka Moment!
Post by: dmjlambert on Jan 06, 2016, 05:52 am
The use of non programming terms makes the platform more acceptable to people in the arts.
Indeed.  The Arduino was born in (and specifically for) the arts.   Let's celebrate that!  10 years of attempt at transforming artists into geeks.  Or at least issuing artists their geek badges (and some of them magic smoke badges).
Title: Re: A Eureka Moment!
Post by: yendis on Jan 06, 2016, 08:40 am
Just wondering what values you set your D to. Most of the time, a non-zero D leads to jittery responses since it draws from the change of values over short period of time. I usually only use P and I and only use D if the single is relatively clean and I need a faster response.

I did not implement a D term in my code.  One of my causes of erratic behaviour was overflow of a variable to a negative number - newbie errors. Also poor resolution was not helping - initially I went to a float variable but kept getting ovf in the result - then I changed to long, and use a x10 multiple and this worked - I still dont understand why float would not work.

PS: crOsh: I still stand by my assertion that the language used to program the Arduino is a 'sketch' and not C.  Although it uses much of the C language there are many Arduino specific terms which are not in any C definition that I know of.   It is misleading to call it C.  I am still working my way through all the Arduino specific stuff.
Title: Re: A Eureka Moment!
Post by: raschemmel on Jan 06, 2016, 09:29 am
Maybe they would have less magic smoke if they smoked less magic...
Title: Re: A Eureka Moment!
Post by: liuzengqiang on Jan 06, 2016, 08:30 pm
PS: crOsh: I still stand by my assertion that the language used to program the Arduino is a 'sketch' and not C.  Although it uses much of the C language there are many Arduino specific terms which are not in any C definition that I know of.   It is misleading to call it C.  I am still working my way through all the Arduino specific stuff.

C doesn't define any standard functions such as print or read or delay etc. It is just keywords and syntax (and basic data types). If you are using a C compiler, which you are, you are using C. There is no need to debate whether Arduino IDE uses C, because it does.

Every programming environment has some standard functions to do things on their target systems. If you program for a PC, you probably have functions to call a shell, get path, time, open files on a FAT or NTFS system etc. These don't make your C a "PC programming language". It's still C.

If you read an Eastern Indian cookbook written in English, do you say it's written in "Indian food language" or do you say it's written in English? Yeah, you find curry and Indian eggplant very seldom used in cooking pasta or other western dishes, but that cookbook is still in English, not "Indian food language".
Title: Re: A Eureka Moment!
Post by: raschemmel on Jan 06, 2016, 09:06 pm
Quote
Every programming environment has some standard functions to do things on their target systems. If you program for a PC, you probably have functions to call a shell, get path, time, open files on a FAT or NTFS system etc. These don't make your C a "PC programming language". It's still C.

If you read an Eastern Indian cookbook written in English, do you say it's written in "Indian food language" or do you say it's written in English? Yeah, you find curry and Indian eggplant very seldom used in cooking pasta or other western dishes, but that cookbook is still in English, not "Indian food language".
Well said. I don't think anyone else could have said it better.
Title: Re: A Eureka Moment!
Post by: larryd on Jan 06, 2016, 10:47 pm
Quote
Maybe they would have less magic smoke if they smoked less magic...
lol
Title: Re: A Eureka Moment!
Post by: larryd on Jan 06, 2016, 10:51 pm
Quote
If you read an Eastern Indian cookbook written in English, do you say it's written in "Indian food language" or do you say it's written in English? Yeah, you find curry and Indian eggplant very seldom used in cooking pasta or other western dishes, but that cookbook is still in English, not "Indian food language".
The Arduino Cookbook is written in "Arduino food language" ;)
Title: Re: A Eureka Moment!
Post by: Henry_Best on Jan 07, 2016, 05:52 am
The Arduino Cookbook is written in "Arduino food language" ;)
I wouldn't want to eat any of the recipes in it!
Title: Re: A Eureka Moment!
Post by: GoForSmoke on Jan 12, 2016, 01:07 pm
The moral of my story?  Well I guess patience and perseverance, and watch out for data typing problems.

Whilst I am far from expert, I hope that now I will be able to help someone else at least in some small way on a similar project.


you picked exactly the right part of the forum (the Bar) and I look forward to seeing your input.
Title: Re: A Eureka Moment!
Post by: raschemmel on Jan 12, 2016, 04:28 pm
Quote
you picked exactly the right part of the forum (the Bar) and I look forward to seeing your input.
It was moved by AWOL (don't remember at which reply). Original post was in General Electronics.
I am sure of this because my reply to the OP was that he should post his project in the Gallery.
That post was removed when the GM moved this to here.