There is a slight delay when I press a button to when the motor actually spins, how do I fix this?
Can you post your code so we can see whatâs going on? (Use insert code here
for nice formatting)
nvm it waas from me printing stuff lol
but wait, how would i fix the delay on the print
We canât really help you without seeing your code first.
ok lol,
int ch3 = Controller1.Axis3.position(vex::percent);
int ch4 = Controller1.Axis4.position(vex::percent);
int ch2 = Controller1.Axis2.position(vex::percent);
int ch1 = Controller1.Axis1.position(vex::percent);
FrontRight.spin(forward, (((round(sqrt(abs(ch2 - ch1)))) * ((ch2 - ch1) ^ 2)) / 10.2), vex::percent);
BackRight.spin(forward, (((round(sqrt(abs(ch2 + ch1)))) * ((ch2 + ch1) ^ 2)) / 10.2), vex::percent);
BackLeft.spin(forward, (((round(sqrt(abs(ch3 - ch4)))) * ((ch3 - ch4) ^ 2)) / 10.2), vex::percent);
FrontLeft.spin(forward, (((round(sqrt(abs(ch3 + ch4)))) * ((ch3 + ch4) ^ 2)) / 10.2), vex::percent);
Controller1.Screen.clearScreen();
Controller1.Screen.setCursor(1, 1);
Controller1.Screen.print(((round(sqrt(abs(ch2 - ch1)))) * ((ch2 - ch1) ^ 2)) / 10.2);
Controller1.Screen.setCursor(2, 1);
Controller1.Screen.print(((round(sqrt(abs(ch2 + ch1)))) * ((ch2 + ch1) ^ 2)) / 10.2);
Controller1.Screen.setCursor(3, 1);
Controller1.Screen.print(((round(sqrt(abs(ch3 - ch4)))) * ((ch3 - ch4) ^ 2)) / 10.2);
Controller1.Screen.setCursor(4, 1);
Controller1.Screen.print(((round(sqrt(abs(ch3 + ch4)))) * ((ch3 + ch4) ^ 2)) / 10.2);
i have a bit of delay for some reason
but if i remove the print, it goes away. the problem is, I kinda need the print
Iâm not sure what exactly is causing the delay, but I can see a bit of redundancy which could slow down the code a bit (I donât think itâd be enough to really notice on this small of a scale, but itâs still better to get rid of it).
You can see here the same math is done twice in both of these commands:
FrontRight.spin(forward, (((round(sqrt(abs(ch2 - ch1)))) * ((ch2 - ch1) ^ 2)) / 10.2), vex::percent);
Controller1.Screen.print(((round(sqrt(abs(ch2 - ch1)))) * ((ch2 - ch1) ^ 2)) / 10.2);
I would recommend doing it like this:
int FrontRightMotorPower = (((round(sqrt(abs(ch2 - ch1)))) * ((ch2 - ch1) ^ 2)) / 10.2);
FrontRight.spin(forward, FrontRightMotorPower , vex::percent);
Controller1.Screen.print(FrontRightMotorPower);
That way you only calculate the value once, and just reference that value in both commands.
The only other thing that I think could be happening is the code is waiting for the Controller to finish getting the data from the brain before moving onto the next line of code, and the data link between the brain and the controller is slow enough that doing that 4 times in a row causes a noticeable âlagâ to the code which makes the motors not update their speed values as quick.
But thatâs just a theory, Iâm sure someone who actually knows what theyâre talking about like @jpearman could give a better answer.
So printing to the controller screen is slow. In order to combat this I would recommend starting a thread and putting the controller print statements there. If you scroll through the tutorials or examples in vexcode pro v5 there should be one on how to start and use a thread.
Code from examples
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: VEX */
/* Created: Sun Oct 06 2019 */
/* Description: This program will run a thread parallel (at the same time */
/* to main. */
/* */
/*----------------------------------------------------------------------------*/
// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"
using namespace vex;
// myThreadCallback is a callback function that can be registered to a thread. In
// this program, it is registered to 'myThread'.
int myThreadCallback() {
int count = 0;
while (true) {
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("myThreadCallback has iterated %d times", count);
count++;
// You must sleep threads by using the 'this_thread::sleep_for(unit in
// msec)' command to prevent this thread from using all of the CPU's
// resources.
this_thread::sleep_for(25);
}
// A threads's callback must return an int, even though the code will never
// get here. You must return an int here. Threads can exit, but this one does not.
return 0;
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Declare and assign myThread's callback to 'myThreadCallback'.
// The thread will start as soon as this command is called.
thread myThread = thread(myThreadCallback);
// Print from the main thread to show that it is running at the same time as
// 'myThreadCallback'.
int count = 0;
while (true) {
Brain.Screen.setCursor(2, 1);
Brain.Screen.print("main has iterated %d times", count);
count++;
wait(25, msec);
}
}
This, thereâs a forced 50mS delay between calls to Controller1.Screen.print.
(and there are only 3 lines)
correct solution. save the values you want to print into some variables and print in another thread.
or try and print everything on one line.
how do another thread, lol, im dum
Click the âcode from examplesâ in the post you replied to or look in the examples section of vex code pro v5
? what do yiu mean by that
Read through this example code
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: VEX */
/* Created: Sun Oct 06 2019 */
/* Description: This program will run a thread parallel (at the same time */
/* to main. */
/* */
/*----------------------------------------------------------------------------*/
// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"
using namespace vex;
// myThreadCallback is a callback function that can be registered to a thread. In
// this program, it is registered to 'myThread'.
int myThreadCallback() {
int count = 0;
while (true) {
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("myThreadCallback has iterated %d times", count);
count++;
// You must sleep threads by using the 'this_thread::sleep_for(unit in
// msec)' command to prevent this thread from using all of the CPU's
// resources.
this_thread::sleep_for(25);
}
// A threads's callback must return an int, even though the code will never
// get here. You must return an int here. Threads can exit, but this one does not.
return 0;
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Declare and assign myThread's callback to 'myThreadCallback'.
// The thread will start as soon as this command is called.
thread myThread = thread(myThreadCallback);
// Print from the main thread to show that it is running at the same time as
// 'myThreadCallback'.
int count = 0;
while (true) {
Brain.Screen.setCursor(2, 1);
Brain.Screen.print("main has iterated %d times", count);
count++;
wait(25, msec);
}
}
so thread is kind of like a event