Teaching C++

Hi CD,

This year I was my team’s Only Student Programmer, and it looks like things are going to be that way next year too. I want to teach some other team members C++. WPILib is nice and makes development very easy, but I think it will pose a challenge for zero experience programmers who aren’t ready to jump into OOP, indirection, memory management, etc. We will be using VEX in the preseason, and the EasyC API is even easier than WPILib, so I plan on starting with C. Before I think any further, do you have any advice on teaching programming? Teaching in general? The timeframe required to really know C?

Thanks.

Honestly for anyone with even a little programming experience around a month-1.5months to REALLY know it. For anyone with next to nothing but a very logical mind 2-2.5 months. and Anyone else 3-6months. To know it enough to program a robot. (basiclly to just know the Libraries, how to use them, and basic syntax).

(This is based more on Java and C++ than C.)

For writing barebones teleop, that’s definitely something you can do in a month or two.

In terms of really knowing C/C++, I’d consider that to be an optimistic timeframe, though this definitely depends on how much time you, the teacher, and your students are willing to commit towards the end goal of teaching whatever, what projects you’ll have them go through, as well as their prior experiences.

I can attest to this. I started learning C++ at the beginning of this year in order to program the robot, due to the lack of any other programmers. I have had experience with various languages throughout my life, but never really stuck with one.

I quickly learned enough to program our drive train and all, and I feel confident that I have the ability to program most anything my team could need. But I still find myself frustrated by the fact that I don’t really KNOW C++. I use C++ to control the robot, but I don’t fully understand how C++ works, and would likely find myself unable to write any kind of non-robot program without the assistance of google.

From year to year, our team always have this problem. If we are lucky, we will have one or two students who already have some backgrounds on programming. Depending on when they join the team, we are luck if we have 2 to 3 years of their service. Then they graduate. Ideally, while they are in service, we would train less experience programming students to take over after they graduate. In reality, this doesn’t always work out. Our school is a very small school with only a few hundred students. We are lucky if the FRC team has over 20 members. This year we have 18 and only a handful are interested in programming. A lot of them have very little or no background on programming. Even if we train some of them, some students may decide to quit robotics after a year or so. In order to make this work, we need a way to rapidly train students. A few years ago, their robot code was monolithic. Everything was written from scratch year after year. The code was hard to understand so it is hard to learn from reading it. One of the ways we use in recent years is to preserve knowledge continuity by developing a robotics library. This library is written as generic as possible so that it can re-used year after year. For less experience programmers, they would learn how to use this library. Reading past code that used this library is a simple way to start. It is a lot faster on-boarding than teaching them how to write the entire code from scratch. For more advanced programmers, they will learn the internals of the library and may even contribute to debugging, maintaining and extending the library year after year. This past summer, we even built a smaller form factor practice bot. It is basically a 24" x 24" running base. Small enough for students to take it home if they need to gain some experience in coding and debugging for it. All of these are more focused on teaching robotics programming, not necessarily just teaching C++. For teaching C++, we supplement it by recommending students to learn the basic language from some internet sites. It is generally difficult to teach a bunch of students who have different levels of programming skill. When you teach something too basic, intermediate or advanced programmers will lose focus. When you teach something more advanced, you will lose the beginners. So each individual student must bring themselves to a more uniform level. We usually start with doing an introduction to the C/C++ language session for really beginners. Then point them to some internet sites for more detail information. Then we start to teach how to program the robot using the library, showing code from past years and explaining the design and structure of the code. Then for whoever are interested and can handle it, we will talk about more advanced topics and how each library module works. In between all these, the small form factor robot is available for demoing concepts and hands-on experiments.

It depends how in-depth you really want to get. We don’t go too much into detail with our students because it’s not really necessary to just get the robot running. This is more or less what we cover with our students:

  • What an integer is
  • What an object is
  • How to declare, set/initialize, etc. both of the above
  • if/else statements
  • What functions/methods are
  • How to declare/write, call, use, etc. a function or method
  • An overview of what WPILib is and how to use it (i.e. how to figure out what functions you can/should call with the different objects)
  • Some of the most commonly used classes, like Victor, Joystick, Solenoid etc.
  • How to declare a pointer and how it changes how to call its functions (using the -> instead of .), though we don’t really go into what the difference is unless someone really wants to know
  • How to read inputs from sensors (analog and digital from both the DS and the robot itself) and use those values
  • Where to put the different parts of the code (i.e. what should go in our Shooter class vs. our DriveBase class and where to put autonomous stuff vs. teleop stuff)

This year, we ran two Saturday workshops for 25 and 103 to go over programming stuff. The first session was for programming basics, so just how to write simple code in general. The second session focused on WPILib and how we use it to write code for the robot.

To help drive it all home, we work with the students have them rewrite the code for the previous robot. In our case, we had each student assigned to a different system on the robot, so one student was responsible for getting drives and brakes to work while another did the pneumatic things. They’ll ask us questions and we lead them in the right direction (like “Remember when we talked about the Joystick class and the different functions it has? Try looking for something to help you in there.”) but we won’t do any of the work for them.

I find that a constant roadblock in my learning C++ on my own is the IDE…
I have numerous books on C++, and have looked over several comprehensive online tutorials. The problem is that I always have trouble with the IDE, either it wont stay installed, I can’t figure it out, it doesn’t work well; and in the end I always forget anything I had learned.

This year I will be trying to teach younger members about programming at the same time as teaching them some basics about electronics.

We will be using arduinos, and doing activities with them that involve controlling motors and reading / interpreting sensor data. I feel that attaching the programming knowledge to a small scale physical demonstration might help them remember it better. Now, the arduino’s programming language isn’t exactly C++, but it is similar enough that they can go on from the arduino activity and start reading over our 2012 robot code, while me and the other existing programmer help them relate things in it to the concepts they learned on the arduinos. We will then re-write the robot code (almost) from scratch, having them offer suggestions for improvement. This should give them a decent understanding of the structure/organization of code for FRC robots, as well as a basic understanding of how various physical electronic components that will be controlled by the code will work, such as jaguars, victors, servos, gyroscopes, ultrasonic rangefinders, etc.

yes, it is. It’s compiled with gcc. Do you mean programming an arduino uses a different library than programming a cRIO?

I agree with you about the IDE – I don’t like IDEs at all. I prefer to use vim and make. Learning a different IDE every few months just isn’t appealing to me.

Ohh… Derp. I just remember reading that the arduino language was something “based on Wiring”, but digging a tad deeper I see where it says that it’s implemented in C/C++. I suppose this just further proves that I don’t really ‘know’ C++, I just dabble in so many languages that I retain enough knowledge to learn any language very quickly, but usually only for a specific purpose / project.

I didn’t mean to say that it uses a different library (well, at least I think I didn’t) but I suppose it does. But I think it should still be useful in teaching some of the basics of programming the robot, at least in terms of how sensors and other hardware can be interacted with through programming.

We teach C++ by giving members websites and software like Visual studio. We encourage being able to learn things on your own and offer help and explanations whenever needed. So far, our programming team is the captain, the apprentice, and a past member of the team who helps us substantially. I’m hoping to get a couple more people in the programming department. The school also has a C++ class and any member of the team is welcome to take it. We don’t discourage anyone from learning programming.

C/C++ can be used to do some very very complex things, mastering them can take many years. There are a lot of things that you don’t even realize you don’t know until you understand compilers and language runtimes and processor/memory/bus/etc architecture. Every time I work on a project I learn something new about C (usually pointers and memory layout and optimization).

That being said, you really don’t need to know all that much to program a FRC robot to do some basic stuff. You don’t really even need to know much of C++, just the C-style syntax and pointers. This list is a great start. Once you have the syntax and variable basics down, I would say spend time understanding object oriented programming and how you can combine WPIlib classes with your own custom code to make very powerful classes. Also, you MUST understand the difference between when things are on the stack and when they are on the heap.

Most importantly, program a robot. Get a minimal set of code, say your robot.cpp that extends IterativeRobot. Write the few lines of code to get a tank drive going. Then get arcade drive. Make some buttons trigger solenoids (no fancy state machines yet). Hook up a potentiometer and code up an Analog channel and do a printf of the getVoltage and getValue() * 5/1024 (notice they are different! very important). Then hookup an encoder and print those values. Being able to use sensors and drive a tank drive in a few hours work (for a beginner) is so rewarding!

Start programming things ASAP. After mentoring for years, its amazing how much you think you taught someone via lecture-style learning and then you see them fail miserably in doing the most basic task. Most importantly, don’t let someone struggle with something syntactic or basic forever, it is very demoralizing and you are wasting both of your time. Help them out and they will understand.

I learned the programming for the robot by starting on labview (3 yrs ago, 8th grade) and then took the school’s programming class (2 yrs ago and has basically only seniors) and then explored plenty more on my own.

The school’s class (taught by the AP Calc teacher who know a tad of programming) starts using Microsoft Studios like 2006 or something (C++). Then the second half of the semester class, he gets students doing some projects with Just BASIC which uses BASIC instead of C++.

Now this past year I have become quite profficient with programming and I needed to teach someone else. One of the things I’ve started was Video tutorials cause our mentor (AP Physics teacher) had started doing that and really likes it. But the person I was teaching had never done programming before and I didn’t have time to go through the whole Microsoft Studios 2006 thing. So instead I took him straight into Just BASIC to teach him the ideas of integers and mathematical operation and for loops and while loops. Then took him to the WindRiver compiler to relate it to the robotics code.

Now that I have done more projects with the Arduino, I might start with that instead but I’m not sure. Also I helped teach a few sessions of the C++ programming class and got them to work on a project to try and make the robot autonomously go around the room without sensors… Almost made it, but we did make enough dents in the walls.

BTW that robot was ~1 ft wide by 2 ft and the frame consisted of a sheet plastic cutting board. It was a double decker and had banebot motors for the drive.

I take a slightly different approach. I run a class on C programming before build season starts. I start out by just teaching the language semantics and syntax. Then I work on logic, flow control, how to break down a problem into smaller steps, etc. I feel that it is more important for students to learn how to program than the specific language. C has a fairly simple syntax.

As people have mentioned you’re filling in a framework, which is basically C syntax within a C++ function.

As we get closer to the start of build season, I walk students through the framework - we use the IterativeRobot class, and show how to use the various sensors, and other objects required to get the robot up and running. As they become more familiar with the various constructs, we talk about processes, memory management, timing, PID control loops, etc. but only with the students that show real interest. Some times it’s only one or two students. This year we had two freshmen that really did a great job, and a hand full of others students who mostly watched them work.

Now there’s where I get hung up. I really don’t have a very good understanding of any of the real technical aspects of the language, but I still have to teach the language to new team members. I suppose I could try to learn these things from online tutorials and the like, but I feel like they are the kind of thing that’s better to learn by being taught…

While I’m typing, I might as well ask. Can you recommend any online tutorials that do a good job of explaining the parts of the language that go beyond syntax? (which, at this point, is the only thing that I really know well).

I enjoy reading these books:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

They’re free online but you can buy hard copies also. They’re pretty old but understanding the first book will give you a deep understanding of C++. The second one talks mostly about the STL so isn’t really relevant to FRC.

Thanks. From what I’ve read so far those books seem like they’ll help quite a bit. Older sources seem to have much more straightforward explanations than many newer ones…

This book does a very good job of teaching programming. It uses python because it’s a simple language to pickup, but once they’ve finished it you can teach them C++ and they’ll have a much higher success rate than just going straight into C++.

I’ve found that just teaching people the language isn’t good enough. You have to teach them how to think like a programmer.

Exactly. The other issue is that it’s not sufficient to think like a programmer, and know the language. You need to have some understanding of mechanical, and electrical systems. Coding up a motor to run is all well and good, but understanding that the motor needs to be driven to hold an arm in position against an opposing force (gravity :)) is another thing.

A number of the items I mentioned are functions of the operating system that is being used, and the hardware. There are books on VxWorks that explain how the OS works, and what features are available. There are books on control theory but most of the ones that I have are not really for novices. For more info than you’ll ever want to know about C++, you can use Bjarne Stroustrup’s book. I always believe in going straight to the source, but for a novice programmer it can be a daunting book.

Also, I agree that Python is another viable language for teaching how to program. The hello world program is probably the easiest and clearest of any language :slight_smile:

This is probably one of the only reasons that I am able to program my team’s robot as well as I am, despite my extremely limited knowledge of C++.

One year our team had a programmer who was practically a java god. They knew java inside out, it was crazy. But they had little to no understanding of any of the mechanical or electrical systems on the bot… Our robot didn’t have working code until I took it home and learned enough java to fix it.

This is one of the reasons that we are now making sure that anyone involved in programming is also heavily involved with other things as well. This is also the main reason that I am planning to use the arduino to teach programming, so that kids can learn how programming can be used to interact with physical things.

Already knowing Java and VB.NET, It was very easy for me to pick up C++. I learned all of the basics in a month. I used learncpp.com. They have great explanations and structured lessons with quizzes at the end. Did I mention it’s free?