|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Strange but...need fresh eyes
We've been trying to figure this one out for a couple of days. Here is the code in question:
p1_y = (unsigned char) (((double) p1_y + (double)127)*.75); if (p1_y > 250) { p1_y = (unsigned char)250; } PrintToScreen ("P1_y is %d\n", (int)p1_y); The first line is a conversion we need to use to make our steering wheel paddles work. p1_y is the value sent to the drive motors. What is happening is the wheels spin forward until the upper limit (250) at which point they immediately spin backwards or stop. Output from the print line reflects this condition, increasing to 250 then converting to 1 or 127. We thought maybe the steering wheel had started to send the wrong signal at its limit, but changing the limit set point (i.e. from 250 down to 245) results in the same thing. Any ideas? |
|
#2
|
||||
|
||||
|
Re: Strange but...need fresh eyes
Quote:
Does p1_y go from -128 to 127, or from 0 to 255. If its from 0 to 255, then adding 127 definitely takes you out of the range of an unsigned char. If its from -128 to 127, then it seems that saving (127+127) to a signed char would also result in overflow. If you want to scale by 75% you can multiply by three, then divide by four: p1_y = 3*(p1_y + 127)/4; No need for floats. |
|
#3
|
|||
|
|||
|
Re: Strange but...need fresh eyes
Quote:
![]() |
|
#4
|
|||
|
|||
|
Re: Strange but...need fresh eyes
Another idea I had was to maintain my drive variables as int to avoid overranging and then of course make sure they are 0-254 before sending them to the PWM ports. That way if there is any spurious or temporary overranging, I won't lose control of the variable. Because no matter how you look at it, if you take and unsigned char with a value > 127 and add 127, then you have over ranged even though subsequent operations are designed to bring it back into range. With ints this shouldn't be a problem right? Unless I really, really really, go out of range.
|
|
#5
|
||||
|
||||
|
Re: Strange but...need fresh eyes
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strange Behavior | Gary Bonner | Control System | 17 | 09-11-2005 19:01 |
| Strange products upon running code... | neilsonster | Programming | 17 | 10-08-2005 00:20 |
| strange error | Anthony Kesich | Programming | 2 | 25-02-2004 18:55 |
| "Regional Competition Edition" of Fresh From the Forum | Ken Leung | CD Forum Support | 5 | 21-03-2002 08:21 |
| "Motors and Drive train edition" of Fresh From the Forum | Ken Leung | CD Forum Support | 6 | 29-01-2002 12:32 |