Programming Tips for C/C++

The Rocketry Forum

Help Support The Rocketry Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

RAHagen

Well-Known Member
Joined
Jun 23, 2014
Messages
647
Reaction score
15
I've been seriously thinking about building a rocket that uses digital stabilization for the last couple weeks. I've read nearly all the documentation I could find about this subject and think I've got a handle on the concepts. The only thing I still need to get better at is my programming skills. The only pre-programmed system I've seen is the Eagle Guardian, and I personally don't think the system corrects quickly enough for guiding model rockets. I was thinking I might want to write the initial programming on my Arduino Uno, and then, once I get the program finished, transferring it to a smaller board like the Arduino nano. The main problem is that the sum of my C/C++ programming knowledge stops at using an Arduino Uno to move a servo with a potentiometer... :confused: I'll have to work on that. Do you guys have any good links or books that can help with C/C++ programming?
 
The Bible for C is K&R, I still occasionally refer to it, though the pages are falling out! Back when I was doing a bunch of C++ reading, I really liked "Effective C++", by Scott Meyers. Now I just write the stuff, and check one of the online guides for obtuse language features.

If you're writing real-time stuff, be careful with C++, it can be written efficiently, but can quickly bloat - Remember this quote from Bjarne Stroustrup - “C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.”
 
And just remember where your first cell in a matrix starts... it's not intuitive.
 
The Bible for C is K&R, I still occasionally refer to it, though the pages are falling out! Back when I was doing a bunch of C++ reading, I really liked "Effective C++", by Scott Meyers. Now I just write the stuff, and check one of the online guides for obtuse language features.

If you're writing real-time stuff, be careful with C++, it can be written efficiently, but can quickly bloat - Remember this quote from Bjarne Stroustrup - “C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.”

I started learning c one week ago and it is a nice language. The C Programming Language, 2nd Edition by K&R is THE resource to start. I also recommend Learn C the hard way if you are a Linux type.
 
Last edited by a moderator:
ah Robert, you are about to step into the wonderful world of controls & feedback...not for the faint of heart!!! might look at this paper and brush up on control loops and PIDs (here is a wiki intro).

Also found this link on YORF (via google) some interesting points there
 
I started learning c one week ago and it is a nice language. The C Programming Language, 2nd Edition by K&R is THE resource to start. I also recommend Learn C the hard way if you are a Linux type.

I'm in a similar boat to the OP in that I want to get better with Arduino, and am curious- did you start learning C a week ago from scratch (e.g. no previous programming experience), or are you transitioning from another language?
 
Last edited by a moderator:
The Bible for C is K&R, I still occasionally refer to it, though the pages are falling out! Back when I was doing a bunch of C++ reading, I really liked "Effective C++", by Scott Meyers. Now I just write the stuff, and check one of the online guides for obtuse language features.

If you're writing real-time stuff, be careful with C++, it can be written efficiently, but can quickly bloat - Remember this quote from Bjarne Stroustrup - “C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.”

c++ is a amplifier. It turns pretty good programmers into great ones. And it turns mediocre programmers into really, really bad ones.


Sent from my iPhone using Rocketry Forum
 
I'm in a similar boat to the OP in that I want to get better with Arduino, and am curious- did you start learning C a week ago from scratch (e.g. no previous programming experience), or are you transitioning from another language?
I am well versed in Python, R, Bash and have explored a dozen other languages. I am a biologist though, not a programmer/computer scientist. I also am an avid Linux user and C feels natural. I am one third into K&R.

I also started learning C because a friend is building the electronic payload for one of my rockets from scratch and has to program it in C. He really doesn't need any help there, but I thought that was the perfect excuse to finally learn C properly. I tried diving into C++ a few times and I think learning solid C foundations before switching to C++ is the way to go.
 
Last edited:
I tried diving into C++ a few times and I think learning solid C foundations before switching to C++ is the way to go.

That would seem like a good idea, but probably not. The syntax will be similar, but C is a sequential language while C++ is an Object Oriented language that is event driven. While C has a beginning and end and flows sequentially, C++ has lots of objects that are activated depending on conditions or events. The biggest issue I had with moving from sequential to event driven languages wasn't the syntax, it was the whole paradigm of how the programs work. I think that is the significant challenge moving from C to C++
 
That would seem like a good idea, but probably not. The syntax will be similar, but C is a sequential language while C++ is an Object Oriented language that is event driven. While C has a beginning and end and flows sequentially, C++ has lots of objects that are activated depending on conditions or events. The biggest issue I had with moving from sequential to event driven languages wasn't the syntax, it was the whole paradigm of how the programs work. I think that is the significant challenge moving from C to C++

Possible. I also have lots more knowledge and experience with object oriented programming now, so I think it would be easier to make the switch.
 
I've read that Arduino is "modified C/C++", so if those two languages are so different, which is the closest relative, worthy of further study to improve ones success and enjoyment with Arduino?


Sent from my iPhone using Rocketry Forum
 
Thanks guys for the information. I'll get reading! I was also thinking of also, next semester, taking a dual-credit programming class at the community college in my town. I don't know what they would teach but if it's something I can learn about programming i'll be interested. I've always been interested in programming, I just was never able to wrap my head around the concept. especially how the processor can understand a programming language at all. :pc:
 
I've read that Arduino is "modified C/C++", so if those two languages are so different, which is the closest relative, worthy of further study to improve ones success and enjoyment with Arduino?


Sent from my iPhone using Rocketry Forum

C++ is almost a superset of C. This means that almost all C code is valid C++ code. They both are each other's closest relative. As I said above, I think that starting with C is the way to go.
 
Thanks guys for the information. I'll get reading! I was also thinking of also, next semester, taking a dual-credit programming class at the community college in my town. I don't know what they would teach but if it's something I can learn about programming i'll be interested. I've always been interested in programming, I just was never able to wrap my head around the concept. especially how the processor can understand a programming language at all. :pc:

Taking a programming class really is a good idea to learn solid bases on which to build. Programming is one of these human activities where the foundations are crucial in order to progress.
 
Okay so I found "The C programming launguage: 2nd edition" on amazon but before I but it I have one more question. I probably should have led with this. Is Arduino's programming language close enough to C/C++ to be easily transferable, or would there be a learning curve between the two? Also, If there is a learning curve between the two, are there any chips on the market that uses the pure C/C++ programming language?
 
Okay so I found "The C programming launguage: 2nd edition" on amazon but before I but it I have one more question. I probably should have led with this. Is Arduino's programming language close enough to C/C++ to be easily transferable, or would there be a learning curve between the two? Also, If there is a learning curve between the two, are there any chips on the market that uses the pure C/C++ programming language?
Arduino's programming language is different, but you can program the Arduino boards with C or C++. Here are two questions on StackOverflow about this:

https://stackoverflow.com/questions/11812850/does-arduino-use-c-or-c
https://stackoverflow.com/questions/5931572/how-is-programming-an-arduino-different-than-standard-c

If you have no previous notion of programming, there will be a very significant learning curve for learning a language. Even more so with C or C++ than for example Python. If you can

There are chips that you can buy and build modules from scratch with. You have to be pretty good at both electronics and programming to do such a project though.

I guess it may help us understand what your needs are if you told us more about yourself, you background in electronics and programming, your operating system of choice (Windows, Mac, Linux or something more exotic) as well as you intended project. What kind or experience do you have with similar simpler projects?
 
Okay well, like I said in my first post, the sum of my programming knowledge is programming a servo to move by turning a potentiometer through an arduino. I got the Arduino starter set and worked my way through their little activity book, but really that's all the experience I have in electronics. At the moment I am nowhere near the level of experience required to write the program I need, let alone make the chip myself, however I've always wanted to learn more about the concept of programming I just never knew where to start.

This project that I'm wanting to start will be a really long one, however I'm hoping that by the end I will have learned a lot, and be able to use it on other projects later on.
 
Okay well, like I said in my first post, the sum of my programming knowledge is programming a servo to move by turning a potentiometer through an arduino. I got the Arduino starter set and worked my way through their little activity book, but really that's all the experience I have in electronics. At the moment I am nowhere near the level of experience required to write the program I need, let alone make the chip myself, however I've always wanted to learn more about the concept of programming I just never knew where to start.

This project that I'm wanting to start will be a really long one, however I'm hoping that by the end I will have learned a lot, and be able to use it on other projects later on.
My guess is it would be awesome for you if you could find someone near you who could sort of mentor you through such a project. Learning to program requires dedication. For a lot of people, it is one of the most satisfying things you can do. It is worth starting in the proper direction though since it is easy to become lost or discouraged so, once again, I would advise you find someone who you can talk to in person and who can help you getting started. Otherwise, all the answers are of course on the internet but when you start, you often don't even know what the proper questions are.
 
One of the nice things about working with an Arduino is that you have access to a lot of existing code. That's intentional because the main goal of the Arduino is to open the technology up to people who aren't engineers or software developers.

So, you may be able to find some code that does something similar to what you need and tweak it for your application. A little knowledge of programming upfront will certainly help, but you might find it more productive to "dive in" and look at the existing code and refer to books or websites as needed to understand it.

-- Roger
 
That would seem like a good idea, but probably not. The syntax will be similar, but C is a sequential language while C++ is an Object Oriented language that is event driven. While C has a beginning and end and flows sequentially, C++ has lots of objects that are activated depending on conditions or events. The biggest issue I had with moving from sequential to event driven languages wasn't the syntax, it was the whole paradigm of how the programs work. I think that is the significant challenge moving from C to C++
Actually, both languages are sequential. Event driven paradigms are a higher level construct, used in many frameworks and/or operating systems. Nothing in these languages require, or even natively support events. Both languages, though, can be used to implement an event driven system, I've done it in a number of products.

That said, the encapsulation and polymorphism of C++ (OOP) adds a powerful, (and potentially complicated) paradigm on top of C. Unfortunately, most (all) of the weakness of "C" are still there, thus the ability to blow one's leg off. C++ isn't necessarily a good language to start with, depending on one's experience with other languages, and the platform being used to develop on.
 
Thanks guys for the information. I'll get reading! I was also thinking of also, next semester, taking a dual-credit programming class at the community college in my town. I don't know what they would teach but if it's something I can learn about programming i'll be interested. I've always been interested in programming, I just was never able to wrap my head around the concept. especially how the processor can understand a programming language at all. :pc:
Good idea, especially if it's something you think you'll want to do more of down the road. This will force you to learn a wide set of the language fundamentals (your programming 'toolbox'), which you can then take to your projects, and other languages down the road. "C" has sometimes been called a "high level assembly language", with its limited high level constructs, and built-in syntax to interact directly with hardware. This can be a curse for beginners *and* large projects, but this same aspect provides a mechanism to understand the underlying processor/memory subsystem. By all means, though, continue to experiment on your own - having some sort of development board that you want to *do* something with makes the whole process more interesting. Good luck! :)
 
Good idea, especially if it's something you think you'll want to do more of down the road. This will force you to learn a wide set of the language fundamentals (your programming 'toolbox'), which you can then take to your projects, and other languages down the road. "C" has sometimes been called a "high level assembly language", with its limited high level constructs, and built-in syntax to interact directly with hardware. This can be a curse for beginners *and* large projects, but this same aspect provides a mechanism to understand the underlying processor/memory subsystem. By all means, though, continue to experiment on your own - having some sort of development board that you want to *do* something with makes the whole process more interesting. Good luck! :)
Thanks dpower!
I went ahead and ordered "The C programming language". It's supposed to be here in around a week, so I'll start reading it once I get it. I'm pretty sure I'm going to do that programming class next semester as well so I'm thinking if what I have isn't enough it'll be a good start! :) Once I actually start to work on my electronically stabilized rocket I'll make sure to make a build thread or something of the sort.
 
I'd also recommend a "Numerical Methods" book. A lot of them provide cookbook methods that are tried and tested. Saves you from having to re-invent the wheel in a lot of cases (some specifically cover PID).
 
I just learned from a friend who did computer engineering... we are working on a large project together. It keeps my coding sense sharp, having someone to bounce ideas off of.
 
My friend suggested using this ressource: Essential C from Standford University. I'm almost through and I find it does a good job at introducing AINSI C and pointing to common pitfalls, all in 45 pages.

After that, it's basically code, code, and code to get more experience.
 
My friend suggested using this ressource: Essential C from Standford University. I'm almost through and I find it does a good job at introducing AINSI C and pointing to common pitfalls, all in 45 pages.

After that, it's basically code, code, and code to get more experience.

Wow 45 pages? I'll make sure to look into that.
 
All languages have plusses and minuses based on the historical period and goals of the developers. C is best thought of as "portable assembler" because it provides only trivial services and abstractions above the machine layer. Because of that, it's the simplest language to write a compiler for and provides the most scope for programmers to micro-optimize.

For large-scale software projects with multiple developers working over years C is a bad choice, but for tasks like this it is well-suited. It's quite easy to learn, although there are a few gotchas that bite people new to the language, mostly because it is so low-level.
 
Back
Top