ROBOTC.net forums
http://www.robotc.net/forums/

Some weird math going on here
http://www.robotc.net/forums/viewtopic.php?f=11&t=5698
Page 1 of 1

Author:  pitaj [ Sun Apr 14, 2013 12:18 am ]
Post subject:  Some weird math going on here

I have a function with some math in it:
Code:
deg = abs(deg);
word millisecs = deg * 17000 / 90;
word millisecs10 = (millisecs / 10);


When I set deg as -30 (at the start), it then becomes 30 and is then multiplied by 17000 and divided by 90 (to get milliseconds) and then divided by 10 (to get 10s of milliseconds).

When I look at the debugger, it shows that millisecs is -158. What?!! Where does this come from? There isn't even a negative number in there.

If someone can tell me how this is happening, I'd be very happy. Thanks in advance.

Author:  mightor [ Sun Apr 14, 2013 1:32 am ]
Post subject:  Re: Some weird math going on here

We need more code, it could be an overflow. What types are the variables?

= Xander

Author:  pitaj [ Sun Apr 14, 2013 11:56 am ]
Post subject:  Re: Some weird math going on here

Full code:

Code:
#pragma config(ProgramType, NonCompetition)
#pragma config(Sensor, in1,    bumper,         sensorTouch)
#pragma config(Sensor, in2,    baseRot,        sensorQuadEncoder, int3)
#pragma config(Sensor, in4,    elbowRot,       sensorQuadEncoder, int5)
#pragma config(Motor,  port1,           base,          tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor,  port2,           shoulder,      tmotorServoContinuousRotation, openLoop)
#pragma config(Motor,  port3,           elbow,         tmotorServoContinuousRotation, openLoop)
#pragma config(Motor,  port5,           claw,          tmotorServoContinuousRotation, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// ****ROBOT ARM ****
int ON = 127;
int OFF = 0;
int REV = -127;
// rotate the base turntable
void rotBase(word deg){
   word neg = deg / abs(deg);
   motor[base] = (ON / 5 * neg);
   while(abs(SensorValue(baseRot) * 2 / 11) < abs(deg)){}
   motor[base] = OFF;
   SensorValue[baseRot] = 0;
}
// rotate the elbow
void rotElbow(word deg){
   word neg = deg / abs(deg);
   motor[elbow] = (ON*neg);
   while(abs(deg) > abs(SensorValue(elbowRot))){}
   motor[elbow] = OFF;
   SensorValue[elbowRot] = 0;
}
// rotate the shoulder
void rotShoulder(word deg){
   word neg = deg / abs(deg);
   // motor[shoulder] = (ON*neg);
   int degree = neg * deg;
   int millisecs = 30 * 17000 / 90;
   int millisecs10 = (millisecs / 10);
   wait10Msec(millisecs10);
   motor[shoulder] = OFF;
}
// open the claw
void openClaw(){
   motor[claw] = ON;
   wait10Msec(100);
   motor[claw] = OFF;
}
// close the claw
void closeClaw(){
   motor[claw] = REV;
   wait10Msec(100);
   motor[claw] = OFF;
}
task main()
{
   // openClaw();
   SensorValue[baseRot] = SensorValue[elbowRot] = 0;
   // while(SensorValue(bumper) == 0){}
   // rotBase(30);
   // rotElbow(30);
   rotShoulder(-30);
   // closeClaw();
}


So my robot looks like this:
Attachment:
File comment: robot diagram
robot.png
robot.png [ 32.65 KiB | Viewed 5780 times ]


As you can see, only two of the motorized joints have quadratures. The claw joint is easy, I can just make a set time for each movement, but the shoulder joint is harder because I don't know how to calculate the time of movement based on the # of degrees I want it to move. I know it moves 90 degrees in 17 sec, but I don't know how to convert say, 30 degrees into 5.6666667 sec without the Math going all weird. I have quite a bit of programming experience, but it seems like RobotC doesn't work like C++ or Java when it comes to Math.

Thanks in advance.

Author:  Eddie [ Sun Apr 14, 2013 12:05 pm ]
Post subject:  Re: Some weird math going on here

I'm not sure how RobotC handles the use of the word datatype (I'm guessing that it is just a short), but have you tried replacing it with a float? The math you have in the first post should result in 566.66 and at the very least you're going to be truncating the repeating .6

Author:  pitaj [ Sun Apr 14, 2013 12:13 pm ]
Post subject:  Re: Some weird math going on here

So I changed the rotShoulder function to this:
Code:
void rotShoulder(word deg){
   word neg = deg / abs(deg);
   // motor[shoulder] = (ON*neg);
   int degree = neg * deg;
   float millisecs10v1 = 30.00 * 17.00 / 90.00 * 100.00;
   // word millisecs10 = (word)(secs * 100.00);
   word millisecs10v2 = (word)millisecs10v1;
   wait10Msec(millisecs10v2);
   motor[shoulder] = OFF;
}


And now in the debug it shows this:
Attachment:
screenshot.46.jpg
screenshot.46.jpg [ 148.84 KiB | Viewed 5779 times ]


And after I close out of the box, the variables in the debug are:
Attachment:
screenshot.47.jpg
screenshot.47.jpg [ 25.42 KiB | Viewed 5779 times ]


As you can see, millisecs10v1 = a very small number. It should equal 566.66667. Does anyone know why this is happening?

If I could just get that one operation to work, everything would be good.

Author:  mightor [ Sun Apr 14, 2013 1:19 pm ]
Post subject:  Re: Some weird math going on here

Avoid using "word" and stick with "int" instead (they're the same). Anyway, an int is 16 bytes and will overflow when you multiply 30 by 17000. So use longs, which are 4 bytes long.

= Xander

Author:  pitaj [ Sun Apr 14, 2013 4:04 pm ]
Post subject:  Re: Some weird math going on here

The compiler gives an error that says:
"**Error**:'long' type variables not supported on platform"

Author:  mightor [ Sun Apr 14, 2013 4:20 pm ]
Post subject:  Re: Some weird math going on here

You're going to have to change your program so your variables stay within the range of a signed int (−32,768 to 32,767)

= Xander

Author:  pitaj [ Sun Apr 14, 2013 4:27 pm ]
Post subject:  Re: Some weird math going on here

mightor wrote:
You're going to have to change your program so your variables stay within the range of a signed int (−32,768 to 32,767)

= Xander


how?

EDIT: They already are.
Newest version of function:
Code:
void rotShoulder(int deg){
   int neg = deg / abs(deg);
   // motor[shoulder] = (ON*neg);
   int degree = neg * deg;
   float millisecs10v1 = (30.00 * 17.00 * 100.00 / 90.00); // should be 566.667
   int millisecs10v2 = (int)millisecs10v1; // should be 566
   wait10Msec(millisecs10v2);
   motor[shoulder] = OFF;
}

Debug output:
Attachment:
screenshot.48.jpg
screenshot.48.jpg [ 24.25 KiB | Viewed 5743 times ]

Author:  Eddie [ Sun Apr 14, 2013 5:22 pm ]
Post subject:  Re: Some weird math going on here

pitaj wrote:
mightor wrote:
You're going to have to change your program so your variables stay within the range of a signed int (−32,768 to 32,767)

= Xander


how?

EDIT: They already are.
Newest version of function:
Code:
void rotShoulder(int deg){
   int neg = deg / abs(deg);
   // motor[shoulder] = (ON*neg);
   int degree = neg * deg;
   float millisecs10v1 = (30.00 * 17.00 * 100.00 / 90.00); // should be 566.667
   int millisecs10v2 = (int)millisecs10v1; // should be 566
   wait10Msec(millisecs10v2);
   motor[shoulder] = OFF;
}

Debug output:
Attachment:
screenshot.48.jpg


Are you using a PIC or a Cortex? If you are using a PIC, long and float are not supported.

Author:  pitaj [ Sun Apr 14, 2013 5:30 pm ]
Post subject:  Re: Some weird math going on here

I am using the PIC microcontroller.

Author:  JohnWatson [ Sun Apr 14, 2013 5:49 pm ]
Post subject:  Re: Some weird math going on here

pitaj wrote:
I am using the PIC microcontroller.


Unfortunately, the long data type is not supported by the PIC microcontroller, which is why you are seeing the current error message. For more information on the different C data types supported by ROBOTC (and which platforms support each type), see our Data Types page on the ROBOTC wiki.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/