View unanswered posts | View active topics It is currently Mon Oct 20, 2014 11:44 pm






Reply to topic  [ 12 posts ] 
Some weird math going on here 
Author Message
Rookie

Joined: Sat Apr 13, 2013 10:37 pm
Posts: 8
Post 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.


Sun Apr 14, 2013 12:18 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3225
Location: Rotterdam, The Netherlands
Post 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
Profile WWW
Rookie

Joined: Sat Apr 13, 2013 10:37 pm
Posts: 8
Post 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 2408 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.


Sun Apr 14, 2013 11:56 am
Profile
Rookie

Joined: Tue Jul 26, 2011 8:19 pm
Posts: 15
Post 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
Profile
Rookie

Joined: Sat Apr 13, 2013 10:37 pm
Posts: 8
Post 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 2407 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 2407 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.


Sun Apr 14, 2013 12:13 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3225
Location: Rotterdam, The Netherlands
Post 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
Profile WWW
Rookie

Joined: Sat Apr 13, 2013 10:37 pm
Posts: 8
Post 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
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3225
Location: Rotterdam, The Netherlands
Post 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
Profile WWW
Rookie

Joined: Sat Apr 13, 2013 10:37 pm
Posts: 8
Post 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 2371 times ]


Sun Apr 14, 2013 4:27 pm
Profile
Rookie

Joined: Tue Jul 26, 2011 8:19 pm
Posts: 15
Post 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.

_________________
Member of the Sarah Heinz House VEX and FIRST teams.


Sun Apr 14, 2013 5:22 pm
Profile
Rookie

Joined: Sat Apr 13, 2013 10:37 pm
Posts: 8
Post Re: Some weird math going on here
I am using the PIC microcontroller.


Sun Apr 14, 2013 5:30 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 591
Post 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.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Sun Apr 14, 2013 5:49 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 12 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.