![]() |
Help, I'm a world class computer programming genius yet I'm totally lost.
Hey guys, I'm looking for help in getting started on things here. My son is a Freshman in high school and is beginning first robotics this year. His team doesn't really have any programming expertise to speak of and it looks like I have been drafted to help out.
I have been drafted because I am an expert professional computer programmer. I have been programming computers in assembly language, C and C++ for about 30 years. One of the first computers I programmed was an Altair, where you had to actually program the computer by setting each instruction in binary via a set of switches and pressing a button. Output was a set of LED lights. So, color me surprised when in 2008 I find that high school kids are still programming low-level embedded systems as 'beginner projects'. I have written a couple of best selling computer games for Electronic Arts, shipped games on numerous platforms and microprocessors, and more recently was the lead engine programmer for the massively multi player online game 'Planetside' for Sony Online Entertainment. So, of course, I am posting a message in this forum, generally to a bunch of teenagers, asking, please, for some help. Now, surely I don't need any help programming, that I know all about. What I want to know, is where in the heck is the documentation for the very, very, hardware specific features of programming these kits for the FRC competition? This week I purchased a VEX Robotics kit and my son, Alex, put the whole thing together. Today we installed MPLAB and experimented with compiling and running some sample code. This raised a huge number of questions. When you read the sample code provided it is filled with 'comments' that make references to obscure acronyms which I cannot find documented anywhere. I currently have the MPLAB compiler, assembler, and linker. I can compile, link, and debug code both on the simulator and the robot controller itself. I have the MPLAB C documentation which, for the record, is quite excellent. I purchased this week a number of books about programming PIC microcontrollers and I am familiarizing myself with them. Still, I have a number of questions. As I read source code, documentaiton, and messages here on the forum it appears to presume a massive amount of knowledge about acroynyms that I can't find documented anywhere. When beginners post questions on the forum they are generally referred to reference material on the C programming language. That's all well and good, but where is the reference material on the arcane oddities of getting the transmitter/receiver to talk to the microchip and the microchip to control inputs, outputs, and motors? Let me give you a specific example. In the sample source code provided with the VEX as a 'learning tool' for new programmers you can find the following section of code: /* Example: The following would generate a 40KHz PWM with a 50% duty cycle on the CCP2 pin (PWM OUT 1): */ /** Setup_Who_Controls_Pwms(USER,USER,MASTER,MASTER,MA STER,MASTER,MASTER,MASTER); CCP2CON = 0x3C; PR2 = 0xF9; CCPR2L = 0x7F; T2CON = 0; T2CONbits.TMR2ON = 1; Setup_PWM_Output_Type(USER_CCP,IFI_PWM,IFI_PWM,IFI _PWM); **/ This is, of course, remarkably non-useful to the new programmer. It raises a long series of important questions. In addition to wondering what the heck a 'PWM' is, and what it's good for, we have a lot of other questions. (1) Where is the source code to the logic that runs when 'MASTER' is set to true? Is this, perhaps, in firmware and we don't have the source? (2) What is 'CCP2CON' and why are we setting it to 0x3C? (No documentation) (3) What is 'PR2' and why are we setting it to 0xF9? (4) What is CCPR2L and why are we setting it to '0x7F'? (5) What is 'TTCON' and why are we setting it to 0? (6) What is 'T2CONbits.TMR2ON' and why are we setting it to zero? Apparently, even in sample code for teenage programmers new to the FRC competition we are to assume they know what 'PWM', 'TTON', "CCPR2', and 'PR2' mean!??? Where is the missing piece of documentation in all of this? I do have some very specific questions: (1) Can I run the same code I compile and link with my VEX controller on the teams FRC controller? (2) Kevin, of kevin.org, provides some amazing looking sample framework code. Can I run this on a VEX or is it for FRC only? Is there enough in Kevin's framework to run a basic robot on a frame, or does it still require a lot of code? (3) Where can I find documentation about all of the device specific acronyms, what they mean, and how are they used? For example, where is an explanation of (A) how to read all of the incoming signals from the remote control and (B) send output signals to all of the motors? Are these the same or different between the FRC controller and the VEX controller? I feel like, after having read enormous amounts of documentation, reviewed a lot of source code, and gone over numerous reference books on how to program the PIC microcontroller, I still don't know how to make a motor turn when somebody moves a joystick button on the controller. Just what am I missing?? Thanks, John and Alex |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
While I have never programmed a VEX Robot, I may be able to help a little bit.
0.) PWM is simply a output, usually to a motor's Victor speed controller. To the rest of them, they look pretty VEX specific to me, I've never seen them on a FRC Robot (though I was looking at code someone else wrote, may be different at lower levels). Thus, to answer another question, you will probably have one heck of a time switching you code from VEX to FRC. (Look at the EasyC program and you have to change the entire format for FRC vs. VEX). p1_y indicates the joystick programmed into the first joystick port on our OI's Y axis value, this may help in setting up an FRC robot moving a bit. Again, all the code I've looked at was written by someone else, so I don't know if they did any special setup to make it work so nice the way it did; but I am glad to offer any knowledge I can. P.S. Welcome to the FRC and/or FTC! P.P.S. If the code is confusing, try EasyC. Your team should have a free license for it, and it is actually a relatively powerful program (still trying to figure out how to use the text editor though...) |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
I've never personally programmed Vex, but I'm told that it's pretty much the same as the FRC controller, so they might be interchangeable.
As for documentation, Kevin Watson includes pretty extensive readmes with all of the code he writes (the C18 3.0+ beta not yet included), as well as pretty extensive documentation for C18 and the RC. All of this can be found on his code repository (kevin.org/frc). If you have specific questions about his code, he patrols these forums pretty frequently. You can either wait for a response here (I wouldn't be surprised if he did), post in one of his threads or email him. One way or another, I'm sure you'll get an answer. As for how close the default code gets you to a working robot, you can get basic drive code in a couple of lines, e.g. Code:
pwm01 = p1_y |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
Excellent post - I was where you are now, 3 years ago.
You may find this document at IFI Robotics interesting Control System Overview. Over at Microchip you will find detailed (!) programming reference materials for the PIC PIC18F8722 resources. If you don't want to climb the detailed learning curve of itsy-bitsy details, I suggest looking at Easy/C (there is a 30-day eval) and the accompanying WPILIB library. |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
Also, the "default code" that comes from IFI contains a few lines of code that copy values from the joystick ports (connected to the Operator Interface) to the PWM outputs. PWM outputs are wired to Victor speed controllers, and these to the motors.
In user_routines.c is the "Default_Routine()" function where most people do their programming. The following lines copy joystick values to the PWM outputs. 127 is the 'neutral' or 'stop' value for the Victor speed controllers ... sending the PWM a 0 value would operate the attached motor at full speed, sending 254 would operated at full speed in the opposite direction. Code:
p1_x = 255 - p1_y;Code:
#define p1_y rxdata.oi_analog01 |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
Basically, you're missing the inputs and outputs :) I had the same problem last year, in that none of the documentation seems to really help much with what someone of our talent would need to know. That being said, I can help.
I would recommend starting with Kevin's code (links here, but it sounds like you already found them: http://www.chiefdelphi.com/forums/sh...ad.php?t=60377) - it's easy to use and pretty simple. For a very basic start, make all your changes to the teleop.c file - that's the one that will "run" when you power up your robot for the first time in your garage. From there, you can branch out to the rest of the code. Now, for the rest of it. Go grab your RC (the board that goes in the robot), the OI (the board that goes with the operator), a joystick, and open up ifi_frc.h from Kevin's code. In that header file is pretty much all of the inputs and outputs you'll need, and everything is mapped to easy to remember names. So, scrolling down the list, you'll find a bunch of p1_, p2_, p3_ and p4_ variables. All of these map, conveniently enough, to the 4 joystick ports on the OI. the analog ones (x, y, wheel, and aux) all have input values of 0-255 - using x as an example, 0 would be when you have the joystick all the way to the left, 127 is in the middle, and 255 is all the way to the right. Next, you see the digital inputs (trig, top, aux1, aux2) - these are your buttons, and are 0 normally, 1 if the button is depressed. So, now you should have all of your inputs! On the RC, you'll see a bunch of pins around the outside - PWM, digital, analog, and the relays are the main ones to worry about. Moving further down the ifi_frc.h file, you'll first see the digital mappings. the digital_io_ variables are used to tell if it's an input or output pin - just set them to INPUT or OUTPUT. from there, you can use rc_dig_in and rc_dig_out to get or set values (0 or 1). Next in the file are the pwm ports, conveniently with variables starting with pwm. These are analog ports, and are used to control things like your drive motors. Set them to any value 0-255. For a drive motor, a value of 0 would be full speed reverse, 255 full speed forward, and 127 stop (depending on the load on the motors, you won't get any change for a range of values around 127). Next are the relay ports (relayX_fwd and relayX_rev). These ones are typically used to power something like pneumatics. In a typical setup, you would want one of them set to 1, the other to 0, but that is really setup and use case dependent. Next are the analog inputs, rc_ana_in. Being analog, these naturally have values rangings from 0-255, just like all the other ones discussed previously. That pretty much covers the basics. If you want to test it, a real simple setup could just be a joystick into port 1, the tether cable between the two boards, and hook up a drive motor to pwm01 (through a victor!), and use the following code in the teleop.c file, in the Teleop(void) function: Code:
pwm01 = p1_x; |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
Thanks for the replies. The main reason I am using the VEX is so that I can experiment programming a fully running robot at home while the high-school team still has a big pile of parts on the floor.
Yes, Kevin's framework looks quite impressive! My questions are this: (1) Will Kevin's code run on a VEX robot? (I can answer this myself in the morning just by compiling and running it of course.) (2) Does his default framework have, out of the box, everything you need to drive a basic frame robot around or do you have to write all of that yourself? As I said in my first message programming, C, or MPLAB, are not the issue. I'm just trying to understand, in code, how you get from point-A to point-B to make the robot respond to inputs and outputs across the board. I found a big missing hole in the sample code provided on the VEX so I quickly got stuck there. I will read the documentation that is included in the links. Thanks, John & Alex |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
Another bit of information ... the PIC microcontroller and runtime environment used in FIRST hasn't changed much in the past several years (since 2004? well, a couple of years ago they changed the PIC part to a different one with more memory, and last year they changed the radio, but both run the same code at the same 10 MHz speed). The rule seems to be "incremental changes" and "don't mess with success - it only causes problems".
So assuming they don't change in years to come, information gleaned is not totally wasted. But since when does technology stand still? |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
John and Alex,
I cut my teeth on the same things as you did. So I know where you are coming from. As a team we use EasyC Pro. Some will say you must use MPLAB as our team has done years before. But last year we did EasyC Pro and it takes the pain out of programming and provides what you need. Has an environment that allows kids to program. We programmed the VEX machines to work out the problems and then moved the code and concepts to the big robots. Our team did well last year, voted in CD having best autonomous mode and made it to Einstein (if you are new that means we went to the Atlanta Championships and were one of the final 12 robots in the competition). I only say that because some look down on EasyC Pro but it lets us proto type quickly and we got results. It has a lot of built in stuff, like encoders etc and other routines that you don’t have to worry about doing and just do the core that you need to do for your team. I would definitely look into it. |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
Quote:
I would say more, but my interests are in turning knobs and filing stuff =p sorry. [offtopic]If you're the J. Ratcliff, programmer I happen to be thinking of: My dad happens to still have his copies of 668 Attack Sub and SSN-21 Seawolf as backups on his PC, and I have MW2 in my small games collection (as well as all the other MW games, I freakin' love mechs) [/offtopic] (Oh, and if you hear that the team needs help with mechanical stuff, encourage somebody to register and send me a PM, I'll help them out if they want, this goes for anybody btw) |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
Quickly
Quote:
Quote:
|
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
A lot of the basics have been covered by my colleagues here, so I'll just throw out some links and try to answer a few of your specific questions. First, you can look through the following pages for several presentations about programming and the control system in general:
Make no mistake, you will need at least a cursory knowledge of the rest of the control system to program effectively. At the very least so you'll know differences in function and purpose of the speed controllers vs. relays, etc. And you'll also be better able to tell if your program is doing something wrong or if one of the mechanical students has unkindly dumping a load of metal shavings into your the electronics. That said, on to some of your more specific questions: The Master source code isn't available for various reasons. I've considered doing disassembly on it out of sheer curiosity, but my brief examination of the binary files we're given to load seem to show jumps to memory areas not included in the dumps we load, so I suspect it'd be fruitless anyways. The good news is that you need to know very very little about that to program successfully. In fact, it is enough to mere know that you receive a data packet from the master every 24ms and that you HAVE to send a packet back for each of those or a watchdog on the master times out and your robot dies. The packets themselves are structs defined in one of the .h files. All those acronyms and their uses are defined in great and painstaking detail in the PIC18F8722 datasheet and the PIC18 reference document. Luckily, you don't need to know a thing about those particular ones to have a successful robot. Code written for in the standard Vex template would not even be source compatible with the standard FRC code. The two systems use different communication protocols between the master and user processor, primarily because of the vastly different IO peripherals and the different controllers. The variable names and aliases are also different for these reasons. You would certainly be able to replicate the algorithms between the systems, but copying code from one to another wouldn't work unless you defined some of your own aliases to map the Vex-specific variable names to FRC specific ones. Finally, since you're an old hand at assembly and code optimization on older, slower processors, a word of warning. If you're thinking of using Fixed Point/Q-Math to speed up calculations on the PIC, be warned that bitwise right shifts are always unsigned, so if you're trying to shift signed variable, you'll either need to offset them into positive numbers or simply divide by the appropriate power of two. I imagine others on here think it odd that I often mention this, but hunting down this particular bug in some code one year was incredibly frustrating. |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
Quote:
To carry the conversation here further, I might ask this. Soon enough I'm going to get the code working to control the motors and such yet I imagine that the programming for robots is a bit more sophisticated than simply 'move joystick send output to motor'? How sophisticated does the code get to deal simply with motion control on the robot? I saw reference to Kevin's code regarding 'encoders', which I can imagine has uses for motion control. What kinds of techniques and algorithms are used purely to make the 'driving around' aspect of a robot more sophisticated? Also, I do have a programming website. (http://www.codesuppository.blogspot.com/) It has no FRC code on it (yet) but I will be making all of my development open-source as I become more familiar with the process. John |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
First, about open sourcing your code. Kevin Watson has asked that his code not be redistributed, so be mindful of that.
For control algorithms, etc, the most important one to understand is PID control, though practically speaking most FRC mechanisms only need P or PD control. These whitepapers should prove helpful for that. The (not-so) brief summary for PID control is that it works like your cruise control works. You pick a set point (cruising speed). The PIC compares this setpoint to feedback it gets from the real world (speedometer). If they don't agree, the PIC applies a control signal in the appropriate direction to correct. In a cruise control, if your actual speed is too slow, it hits the gas. Too fast, it lets off the gas. The algorithm simply multiplies the error by a constant gain and sets the output to this, and you have Proportional control. The remaining terms don't apply directly to cruise control, buuuut... If you were climbing a mountain/left the tire chains on/were dragging a boat anchor, then your car would consistently have an error, since you have to apply SOME gas to over come this, and when you hit your set point, you're applying 0 gas. So you integrate the error as time goes on, multiply by a gain, and add this to your P term. The longer you're missing your set point, the bigger this term, and the more you correct. Integral control. Finally, there's Derivative control. You monitor how fast the error is changing, multiply by a gain, and add. The purpose here is to slow down a rapidly correcting system so that it doesn't overshoot its target. And all that is covered in more detail with better examples in those white papers. Encoders are opto-electrical devices that you can use to determine the position or speed of a shaft, so you can use them for the feedback. You can also use potentiometers, inductive or ultrasound sensors, photo-resistors, or whatever else is appropriate for what you're interested in controlling. Finally, to end my last long winded post of the night, I'd highly recommend reading up on state-machines as mentioned in this presentation. It's a highly useful technique for autonomous programming. Especially with the hybrid mode this year. |
Re: Help, I'm a world class computer programming genius yet I'm totally lost.
I'd add more, but the important parts have already been covered... all within about two hours. Congratulations CD posters on doing an excellent job to help someone new to the field once again.
And if it is any comfort, it took me (programming since C64 days) a while to come up to speed, and our programming mentor (a professional software jockey) a couple weeks to make the switch from computers to robots. One of the great things about FIRST is that there is a lot for us old guys to learn, too! Once you get the I/O figured out, there is a massive overlap in the skill set. Sounds like your on track to become an excllent mentor for this team! Jason |
| All times are GMT -5. The time now is 02:53 AM. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi