ROBOTC.net forums
http://www.robotc.net/forums/

Speed Adjuster Problems
http://www.robotc.net/forums/viewtopic.php?f=11&t=13327
Page 1 of 1

Author:  GrantAlbright [ Thu Feb 04, 2016 6:38 pm ]
Post subject:  Speed Adjuster Problems

I am trying to make a program with tank drive controls. I want to adjust the speed with the remote control. The motors will be divided by the variable Schange. The problem is that the motors don't run when Schange is greater than one.
Here's the code,

#pragma config(Motor, port2, LeftDrive, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port3, RightDrive, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port4, testor, tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

float Channel2; //Right Joystick
float Channel3; //Left Joystick

int threshold; //the accual deadzone

float Schange; //the value the is divids the speed

int speedadjusting()
{
if ((vexRT[Btn5D])==1)
{
Schange = 1;
}
if ((vexRT[Btn6D])==1)
{
Schange = Schange + 1;
}
return Schange;
}

void deadZoneCheck()
{
if ( abs(vexRT[Ch2]) > threshold)
Channel2 = vexRT[Ch2]/Schange;
else
Channel2 = 0;
if (abs(vexRT[Ch3]) > threshold)
Channel3 = vexRT[Ch3]/Schange;
else
Channel3 = 0;
}
task main()
{
Schange = 1;
while(1==1)
{
speedadjusting();
deadZoneCheck();
motor[LeftDrive] = Channel2;
motor[RightDrive] = Channel3;
}
}

Any help would be greatly appreciated.

Author:  Tabor473 [ Fri Feb 05, 2016 2:10 pm ]
Post subject:  Re: Speed Adjuster Problems

Okay so what is happening is a simple case of your code executing multiple times while the button is held down. So imagine that the code runs through 1 time per second and you held the button down for 60 seconds. What would the value be of Schange at the end of the 60 seconds?

The code on the robot runs thousands of times per second so any period the button is held down causes the button to be increased multiple times. Once this gets above 127 the motor will not run at all. What we want is for the variable to only be incremented once per button press. Rather than check if the button is held down what we do is check if the button is held down AND last time it wasn't.

Also currently speedadjusting returning Schange does not do anything so I made it a void function. The function sets the variable Schange so it doesn't need to return it. (I can explain this a lot better if this isn't clear)

Code:
#pragma config(Motor, port2, LeftDrive, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port3, RightDrive, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port4, testor, tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

float Channel2; //Right Joystick
float Channel3; //Left Joystick

int threshold; //the actual deadzone

float Schange; //the value that divides the speed
bool Btn6DLast=false;

void speedadjusting()
{
   if ((vexRT[Btn5D])==1)
   {
      Schange = 1;
   }
   if ((vexRT[Btn6D])==1&&Btn6DLast==0)
   {
      Schange = Schange + 1;
   }
   Btn6DLast=vexRt[Btn6D];
}

void deadZoneCheck()
{
   if ( abs(vexRT[Ch2]) > threshold)
      Channel2 = vexRT[Ch2]/Schange;
   else
      Channel2 = 0;
   if (abs(vexRT[Ch3]) > threshold)
      Channel3 = vexRT[Ch3]/Schange;
   else
      Channel3 = 0;
}
task main()
{
   Schange = 1;
   while(1==1)
   {
      speedadjusting();
      deadZoneCheck();
      motor[LeftDrive] = Channel2;
      motor[RightDrive] = Channel3;
      wait1Msec(50);//Don't hog CPU
   }
}

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/