Quote:
|
Originally Posted by billbo911
As for the declaration of the two variables as "char" instead of "int":
The maximum range of PosError3 is -254 to 254.
This is limited by they following code to -127 to 127 before it is used to create DrvCmd3. At the point DrvCmd3 is created, it can only take on a values of 0 to 254. (Again, I am new to C so here is where I may be mistaken) Both of these ranges of values can be represented by a "char" as well as a "int". I chose the "char" just to save a little memory space. I guess as sloppy as this code is, that really doesn't matter.
Either way, I have seen lots of coding examples where "int" is used as more of a standard, even though a "char" would work. Oh well, I'm still learning, and your input really helps.
Thanks again!!!! 
|
The range of PosError3, declared as a char, is -128 to 127.
The range of the expression assigned to it is larger. When you
assign 254, for instance, and read the value back from PosError3,
you will get -2, not the value intended. Looking at your code,
it looks like the result would be a pwm signal in the dead band,
instead of full forward. Assigning 128 will be read as -128,
and in this case your code will cause the motor to go full speed
in the wrong direction.
In the case of DrvCmd3, you have the same problem, but you are
saved by the fact that the computer takes the misinterpreted 8 bits
and blindly shoves them into the unsigned char, pwm03, so the result is
the one you expect.
As I noted, the issue a visible bug for PosError3, but does not manifest
itself as a bug for DrvCmd3. If you, or someone else, were to modify
the code to somehow test the value of DrvCmd3 and manipulate it
before assigning it to pwm03, the additional bug would surface.
A table of widths, and range, of integer types on the robot controller
can be found in "An introduction to C programming for FIRST robotics
applications." See "C programming for the robot controller"
at the web address
http://srvhsrobotics.org/index.php?g...tarticle&cat=2
for the latest version.
I hope this is helpful...
Eugene