Before going back to my lecturing (once a professor always a professor), I'll point out some things I spotted in some of the posts. The lecture per se resumes at point (6).
(1) Woody, as for the multiplier, if you re-read what I wrote, I think you'll see that I said the same thing. One early version of the script (from someone at Roboteq no less) had (n*100)/70 when it SHOULD be (n*70)/100. I think it was gac3rd who first pointed this out.
(2) Both versions of the script that you (Woody) posted, with two IF constructs or one IF ELSE construct both have a little flaw. You've excluded 0 because you test for < 0 and for > 0. What will happen if the stick was displaced and is now moved back to center? The motor will continue running at its previous setting because 0 is never recognized to reset the motors to 0. So, one of the conditional (just one and it doesn't really matter which) must be changed to a <= or >= so centered-stick is recognized.
(3) A two-IF construct would work (instead of an IF ELSE) but each IF must have its own ENDIF, whereas with IF ELSE and IF ELSEIF you are chaining and nesting within one IF construct. Some professional programmers don't like ELSEIF constructs because it can be harder to spot problems like the excluded 0 in (2) above. Others like to always have an ELSE at the end with no conditional at all because that makes sure that all cases are covered.
Actually, however, neither the two IF nor the IF ELSE construct is necessary for what you're doing here AS LONG AS THE SetCommand lines come AFTER the ENDIF. Thus, if going backwards the values will be scaled and those will be sent to the motors. If not going backwards they will be unchanged. In either case they will be sent on to the motors. By now, John has surely gotten is ENDIF placement figure out.
(4) I think that some of this has already been covered in preceding posts. Pulse and serial inputs are scaled exactly the same way as analog, you just have to call _CIP or _CIS instead of _CIA. In a fancier version of the script, because the Roboteq can auto-switch from one to the other, you would want to first test for which mode is active and chose the _CIx according to which mode is being used. I don't see a anything in the command list that finds out which mode is active (have I just missed that?), so one might have to test with something like the following:
- clip 1.jpg (65.23 KiB) Viewed 19906 times
(This stuff gets real hard to read without indenting. I'm therefore posting code as jpg images with a little formatting.)
(5) VERY IMPORTANT, BUT FROM LATER POSTINGS WHAT I'VE WRITTEN IN THIS PARAGRAPH MAY BE COMPLETELY WRONG!. What you've posted, and what I have written above assumes that _CIx,1 is Throttle and _CIx,2 is Turn. THEY ARE NOT (but maybe they are ???). This is not a MicroBasic thing, but a Roboteq thing. Recently Burgerman tested to see whether _CxI values are upstream or downstream of mixing. They are downstream - if he pushes the joystick straight forward, both :CIP 1 and CIP 2 go up indicating that CIP 1 is not speed - it is motor 1 - and CIP 2 is not turn - it is motor 2. This is great for doing motor compensation, which is why I asked him to run the test, but not so good for speed and turn scaling where we would want to modify Speed and Turn and not Motor 1 and Motor 2.
One can read the raw pot value before mixing, but there is no way I see to change those values and send them back into the Roboteq's calculation stream. That means that to scale turn we have to "decompose" the mixed _CIx values to find turning information. (If _Cix are actually pre-mixing, these calculations get much simpler, but doing motor compensation gets much more complex.)
For scaling speed, we have to find out (using both CIx values) whether we are going forwards or backwards (because we may want to apply different scaling for the two), and how fast we're going, and then scale that and apply the change equally to both motors. For turning, we have to apply half the scaling correction in one sense for one motor, and half in the other sense for the other motor. I wasn't too sure that I was thinking about this right, so I ran some examples in a spreadsheet before starting to convert it to MicroBasic.
- Roboteq scaling.jpg (80.14 KiB) Viewed 19900 times
(6) Lecture resumes.
We now have an idea of what we want our program to do, so it's time to start writing it. While we're at it, however, let's try to structure the program a bit because we're probably going to want to add things and change things as time goes on.
I've put comments between {} braces, but that's not the way MicroBasic does comments - it uses a single quote mark to begin a comment, so don't just go copying stuff from here into a script. Creating a working script isn't my intent in any case; I'm just trying to share some ideas about things that might help write a good script. I don't have a Roboteq controller so I can't test a working script anyway, and I don't want to give you a runable, but untested, script.
First off, I wouldn't actually use the numbers 70 (speed scaling in reverse), 90 (speed scaling going forward) and 35 (turn scaling) where the calculations get done, but would specify some variables at the top of the script so that it would be easy to change them if they turn out not to be optimum.
- clip 2.jpg (10.74 KiB) Viewed 19900 times
MicroBasic assumes all variables are Integer unless told otherwise. Because there are a few Boolean (TRUE or FALSE) variables I'm going to be wanting later on, I'll tell the program what they are here (although in MicroBasic I don't think you are required to do it at the beginning).
ON TO A NEW POST AS ONLY 3 ATTACHMENTS ARE ALLOWED PER POST