View unanswered posts | View active topics It is currently Sat Oct 21, 2017 9:53 am






Reply to topic  [ 2 posts ] 
Speed Adjuster Problems 
Author Message
Rookie

Joined: Thu Feb 04, 2016 6:27 pm
Posts: 1
Post 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.


Thu Feb 04, 2016 6:38 pm
Profile
Moderator
Moderator

Joined: Tue May 19, 2015 3:07 pm
Posts: 91
Post 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
   }
}


Fri Feb 05, 2016 2:10 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 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.