![]() |
Modified default code gives "code violation"
I've been modifying the default code to try to get a basic "joystick sensitivity reducer" working. I'm not planning on using this in our final robot as it significantly reduces the amount of power we can use. What it's supposed to do is subtract 127 from the joystick value and divide the result by two. Here's the code:
Code:
/* Variable declarations (at top of program) below */ |
Re: Modified default code gives "code violation"
Code:
char joystick_input_2 = 0; |
Re: Modified default code gives "code violation"
I don't see anything in the code you posted that would cause a code violation. It would help if you posted your entire Default_Routine and Process_Data_From_Master_uP
Also, since you need joystick_input_2 and drive_output to be signed, it makes your code easier to understand and modify if you declare them as "signed char". Also, I can't remember if the C18 default for char is signed or unsigned. |
Re: Modified default code gives "code violation"
Quote:
Code:
joystick_input_2 = joystick_input - 127;Have you commented out the calls to SlowDrive() to prove that is where the runtime error comes from? I don't know what this particular compiler does but I wonder about this line, Code:
joystick_input_2 = joystick_input - 127;Code:
joystick_input_2 = joystick_input; |
Re: Modified default code gives "code violation"
You probably get code errors because of your printf statements:
Code:
unsigned char joystick_input = 127;To fix, try passing it an unsigned integer explicitly by doing the following: printf("Slow_Drive for pwm01 = %u", (unsigned int)joystick_input); The %u says "print as unsigned". The cast to unsigned int will ensure that the arguments are decoded correctly within the printf function. For those interested, the long explanation: printf takes a variable number and type of arguments. This is contrary to most functions, which have a very specified number and type of arguments (void f(int x, int y) takes two ints, for example). Printf relies on you supplying the correct format characters in the initial string argument to be able to decode the subsequent arguments. This is so that it can grab the right number of bytes off the block of memory representing the passed arguments. If you put a format character for int (%d), it will grab sizeof(int) bytes. If you put a format character for char (%c), it will grab sizeof(char) bytes. And so on. The original poster's code specified %d as the format string, but passed a char. This means the printf code went to grab 2 bytes (sizeof int), but, in reality, the function was only passed 1 byte (sizeof unsigned char). This resulted in printf accessing memory outside the space allocated to the function's arguments. In this case, it resulted in red light of doom..... But that isn't necessarily always the case. The results here really are undefined and unpredictable. There is a chance it would just run, probably printing out incorrect results for the passed unsigned char. Another common mistake with printf is when printing long variables. You need to use %ld (for long) or %lu (for unsigned long). If you accidentally use %d for long, you'll likely just get wrong output, but not red light of death. |
Re: Modified default code gives "code violation"
Ah, thank you Keith.
|
Re: Modified default code gives "code violation"
Quote:
|
Re: Modified default code gives "code violation"
I've got a suggestion for you, if you have the old joysticks with the little wheel on the side, scale your drive up or down based on that. It is probably the most useful function we had on our controls last year.
Also, for scaling, try this: Code:
newval = (oldval-127)*(%scale)+127; |
Re: Modified default code gives "code violation"
Quote:
I am not the one asking the question, I just thanked you for telling me why it was not unsigned. |
| All times are GMT -5. The time now is 14:21. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi