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:
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:
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. 
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:
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. 
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  
how? EDIT: They already are. Newest version of function:
Debug output:

Author:  Eddie [ Sun Apr 14, 2013 5:22 pm ]  
Post subject:  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. 
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  
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/ 