View unanswered posts | View active topics It is currently Thu Jul 31, 2014 8:02 am






Reply to topic  [ 3 posts ] 
Multiplication problem 
Author Message
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Multiplication problem
Hi, it's been a while since I used C so this is probably a really simple problem. I think it is something to do with multiplying an int by a float, the code worked before v3.5 and now doesn't and I though this sort of thing was handled automatically. I found another thread from a while ago with a similar prob where typecasting was mentioned, but this is a bit above my pay grade:-) Anyway, help or suggestions welcome please, the complete code is below.

Why does this work?
Code:
int CurrentRightPower = 1;
float Gear = 0.5;
CurrentRightPower = nMotorEncoder[RightDrive] + 60;
      CurrentRightPower *= Gear;


But this gives a stack overflow (I think that is the right term), when the reading from the encoder is negative then CurrentRightPower is correct, but when the reading from the encoder is positive then CurrentRightPower is -32xxx?
Code:
int CurrentRightPower = 1;
float Gear = 0.5;
CurrentRightPower = (nMotorEncoder[RightDrive] + 60) * Gear;


Complete code:
Code:
#pragma config(Sensor, S1,     LeftButton,     sensorTouch)
#pragma config(Sensor, S2,     RightButton,    sensorTouch)
#pragma config(Motor,  motorA,          TurretControl, tmotorNXT, PIDControl, reversed, encoder)
#pragma config(Motor,  motorB,          RightDrive,    tmotorNXT, PIDControl, reversed, encoder)
#pragma config(Motor,  motorC,          LeftDrive,     tmotorNXT, PIDControl, reversed, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// Project: Jerry
// Function: remote control with two motors acting as joysticks
// Author: Mike McFarlane 06-09-12
// Version: 0-1
// Status: dev

task main()
{
   int CurrentRightPower = 1;
   int CurrentLeftPower = 1;
   int CurrentTurretPosition = 0;
   float Gear = 0.5;
   int Horn = 1;

   // reset motor encoders
   // initial calibration should be done with both joysticks fully forward
   nMotorEncoder [RightDrive] = 0;
   nMotorEncoder [LeftDrive] = 0;
   nMotorEncoder [TurretControl] = 0;
   wait1Msec(100);

   while (true)
   {
      // read encoder and print to screen
      // encoder range; 0 = fully forward, 125 = fully back, 60 = natural neutral
      CurrentRightPower = nMotorEncoder[RightDrive] + 60;
      CurrentRightPower *= Gear;
      CurrentLeftPower = (nMotorEncoder[LeftDrive] + 60) * Gear;
      CurrentTurretPosition = nMotorEncoder[TurretControl];
      nxtDisplayBigTextLine(0, "R: %d", CurrentRightPower);
      nxtDisplayBigTextLine(2, "L: %d", CurrentLeftPower);
      nxtDisplayBigTextLine(4, "T: %d", CurrentTurretPosition);
      // check right button to set motor gear ratio
      if (SensorValue (RightButton) == 1)
      {
         if (Gear == 0.5)
         {Gear = 1.5;}
         else
         {Gear = 0.5;}
      }
      while (SensorValue (RightButton) == 1)
      {
         // do nothing till button released
      }
      nxtDisplayBigTextLine(6, "G: %1.2f", Gear);
      // check left button for horn - toot toot!
      if (SensorValue (LeftButton) == 1)
      {
         if (Horn == 1)
         {Horn = 2;}
         else
         {Horn = 1;}
      }
      while (SensorValue (LeftButton) == 1)
      {
         // do nothing till button released
      }
      // send message to paired NXT
      sendMessageWithParm (CurrentRightPower, CurrentLeftPower, Horn);
      wait1Msec(100);
   }
}


Thu Sep 13, 2012 10:28 am
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 558
Post Re: Multiplication problem
I was playing around with this and found that removing the parentheses solved the problem. ROBOTC by default follows Order of Operations (http://www.robotc.net/wiki/Order_Of_Operations) so the parentheses may have been confusing it. I'll make sure this gets looked into a bit more though. Thank you for the heads up!

_________________
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


Thu Sep 13, 2012 1:51 pm
Profile
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Re: Multiplication problem
JohnWatson wrote:
I was playing around with this and found that removing the parentheses solved the problem. ROBOTC by default follows Order of Operations (http://www.robotc.net/wiki/Order_Of_Operations) so the parentheses may have been confusing it. I'll make sure this gets looked into a bit more though. Thank you for the heads up!


Thanks for the reply John, I'll read the documentation linked and then try again. I've been playing with a bit of Python recently and it is so tolerant of mistakes/idiots, C on the other hand...


Sat Sep 15, 2012 5:22 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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

Search for:
Jump to:  



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