Its a simple question, with a simple answer I hope.
Does a variable, for example, int ‘motorspeed’, need to be filtered within the -127 to 127 range before using a motor output block?
Another way of asking it would be, what would happen if i set a motor to a speed of say 1000? Does the cortex change this to 127 on its own, or will this just not work?
It’s been a while since I’ve programmed, and I don’t have a cortex nearby to test my code. With only a few days later this week to debug my programming before we pack for competition, I’d like to get it near perfect before hand.
No, speeds are not pre-filtered. The GUI will not allow you to set a fixed speed outside the range +/- 127 but if you send a variable then it will give an error on the Terminal window such as
[FONT=“Lucida Console”]Setting Motor Speed with bad value 1000[/FONT]
The easiest way to improve this if you are calling SetMotor is to create a small user function to replace the SetMotor call which will do the limiting.
It’s a little cleaner than what I have written, but I’d have to integrate my motor inversions into it. I normally wait till the end of my program to invert motor directions to keep it simpler.
I’m out of time to clean my code; competition is in a few days. However, I’ll add it into my programming template for next year (if you don’t mind ;))
Quick question about programming:
In the past I have tried to write Proportional Integral loops without success. I can write both pieces fine, and they work on the robot. However, when i try to multiply them by non-integer gain values (K), i run into issues.
So right now I have a simple P loop. Basically it is:
Motor = (Setpoint-Sensor)
It works, but I wanted to tune it better. If I use this, all hell breaks loose:
Motor = (Setpoint-Sensor)/2
My motor variable is a long type, the setpoint an unsigned int, and the sensor a unsigned int.
Is this an issue concerning variable types? Or am I missing something?
Again, I’m using a Cortex with the latest version of EasyC v4 (sorry, i dont have the program here with me for the exact version number)
I wrote them as unsigned since i was using a potentiometer and that’s the variable type EasyC recommends. :rolleyes: So then any math that combines unsigned and signed variables (is that what they’re called?) may lead to issues?
Now that i think about it on the byte sized level that makes sense…pun intended
You need to make sure that unsigned integer operations do not wrap. Google that and you will turn up plenty of programmers with your exact question, here is one result.
I’m not sure why EasyC would recommend unsigned int for sensors, the analog sensors such as a pot will return values in the range 0-1023 (or 0-4095 for the high res version), well within the range of a signed int. I normally use unsigned when, for example, I need a container for information where each bit has meaning or perhaps I plan to use shift operations.
For quad encoder counters I would use long as 16 bit ints may overflow.
That probably saved me hours of guess-and-check debugging and a few sets of gears! It also explains why my motor variable was jumping so high on my feedback display.
Thanks jpearman!
Only a little more programming and tuning left, then I can pack it up and sleep easy for a few days until competition