BrianPeek / legoev3

LEGO MINDSTORMS EV3 API for .NET
Apache License 2.0
137 stars 72 forks source link

Best way for turn a specific number of degrees? #15

Open MikeHouser opened 8 years ago

MikeHouser commented 8 years ago

Hi, my name is Mike and I'm trying to get into the functionality of the API. I have a little robot that drives as 'tank'. Would you please tell me, what is the best way to turn the robot 90 degrees?

public async Task<bool> TurnRight90Degrees() { _brick.BatchCommand.StepMotorAtSpeed(_drivePortRight, _state.SpeedInverted, 200 /* try values here */, BRAKE); _brick.BatchCommand.StepMotorAtSpeed(_drivePortLeft, _state.Speed, 200, BRAKE); await _brick.BatchCommand.SendCommandAsync(); return true; }

also i can't find any hints what this method is for: StepMotorSync

would you be so nice to explain it to me?

thanks! cheers mike

njsokalski commented 8 years ago

To turn your robot 90 degrees, you basically need to do a calculation based on the following values:

  1. Distance between the wheels or treads
  2. Radius of the wheels or wheels turning the treads So here are the basic steps for you:
  3. Determine the distance each wheel/tread needs to travel: distancetotravel=wheelspacingPi(90/360)
  4. Determine the distance 1 rotation of the motor will travel: onerotationdistance=2_Pi_wheelradius
  5. Determine the number of rotations required to rotate your tank 90 degrees: requiredrotations=distancetotravel/onerotationdistance
  6. Determine the number of degrees to rotate the motor: motordegrees=360_requiredrotations So, the final simplified answer is rotate the motors the following number of degrees: motordegrees=(wheelspacing_degreestorotatetank)/(2*wheelradius) I would suggest making this a function that takes degreestorotatetank as a parameter (instead of a function just for 90 degrees), you can also define wheelspacing & wheelradius as constants. Hopefully this helps.