View unanswered posts | View active topics It is currently Fri Dec 14, 2018 2:13 am

 Page 1 of 1 [ 12 posts ]
 Print view Previous topic | Next topic
Some weird math going on here
Author Message
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:
 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

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
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 turntablevoid 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 elbowvoid 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 shouldervoid 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 clawvoid openClaw(){   motor[claw] = ON;   wait10Msec(100);   motor[claw] = OFF;}// close the clawvoid 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 [ 32.65 KiB | Viewed 7483 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.

Sun Apr 14, 2013 11:56 am
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
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:
 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 [ 148.84 KiB | Viewed 7482 times ]

And after I close out of the box, the variables in the debug are:
 Attachment: screenshot.47.jpg [ 25.42 KiB | Viewed 7482 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

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
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

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
Rookie

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

 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 [ 24.25 KiB | Viewed 7446 times ]

Sun Apr 14, 2013 4:27 pm
Rookie

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

 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
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

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
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 updated help documentation and the ROBOTC Forums.

Sun Apr 14, 2013 5:49 pm
Display posts from previous:  Sort by
 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning