|
Page 1 of 1
|
[ 12 posts ] |
|
Some weird math going on here
Author |
Message |
pitaj
Rookie
Joined: Sat Apr 13, 2013 10:37 pm Posts: 8
|
 Some weird math going on here
I have a function with some math in it: 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.
|
Sun Apr 14, 2013 12:18 am |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 Re: Some weird math going on here
We need more code, it could be an overflow. What types are the variables?
= Xander
_________________| Professional Conduit of Reasonableness| (Title bestowed upon on the 8th day of November, 2013) | My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
Sun Apr 14, 2013 1:32 am |
|
 |
pitaj
Rookie
Joined: Sat Apr 13, 2013 10:37 pm Posts: 8
|
 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: 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.
|
Sun Apr 14, 2013 11:56 am |
|
 |
Eddie
Rookie
Joined: Tue Jul 26, 2011 8:19 pm Posts: 15
|
 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
_________________ Member of the Sarah Heinz House VEX and FIRST teams.
|
Sun Apr 14, 2013 12:05 pm |
|
 |
pitaj
Rookie
Joined: Sat Apr 13, 2013 10:37 pm Posts: 8
|
 Re: Some weird math going on here
So I changed the rotShoulder function to this: And now in the debug it shows this: And after I close out of the box, the variables in the debug are: 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.
|
Sun Apr 14, 2013 12:13 pm |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 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
_________________| Professional Conduit of Reasonableness| (Title bestowed upon on the 8th day of November, 2013) | My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
Sun Apr 14, 2013 1:19 pm |
|
 |
pitaj
Rookie
Joined: Sat Apr 13, 2013 10:37 pm Posts: 8
|
 Re: Some weird math going on here
The compiler gives an error that says: "**Error**:'long' type variables not supported on platform"
|
Sun Apr 14, 2013 4:04 pm |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 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
_________________| Professional Conduit of Reasonableness| (Title bestowed upon on the 8th day of November, 2013) | My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
Sun Apr 14, 2013 4:20 pm |
|
 |
pitaj
Rookie
Joined: Sat Apr 13, 2013 10:37 pm Posts: 8
|
 Re: Some weird math going on here
how? EDIT: They already are. Newest version of function: Debug output:
|
Sun Apr 14, 2013 4:27 pm |
|
 |
Eddie
Rookie
Joined: Tue Jul 26, 2011 8:19 pm Posts: 15
|
 Re: Some weird math going on here
Are you using a PIC or a Cortex? If you are using a PIC, long and float are not supported.
_________________ Member of the Sarah Heinz House VEX and FIRST teams.
|
Sun Apr 14, 2013 5:22 pm |
|
 |
pitaj
Rookie
Joined: Sat Apr 13, 2013 10:37 pm Posts: 8
|
 Re: Some weird math going on here
I am using the PIC microcontroller.
|
Sun Apr 14, 2013 5:30 pm |
|
 |
JohnWatson
Site Admin
Joined: Thu May 24, 2012 12:15 pm Posts: 722
|
 Re: Some weird math going on here
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.
_________________Check out our Blog! And our Facebook page! Need help? Take a look at our updated help documentation and the ROBOTC Forums.
|
Sun Apr 14, 2013 5:49 pm |
|
|
|
Page 1 of 1
|
[ 12 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 2 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|