essentially, what’s happening is that the bot moves forwards and backwards, but not side to side, and one of the wheels isn’t working on the spin (it works in fwd/rev.) Does anyone have any working Mecanum code I can take a look at to see what’s different?
is it an issue with the bot or my code?
Take a look at this thread.
Hi, I tried the solution in that thread, and now everything works fine, except I can’t move from side to side. Is there any solution?
-
Did you choose the Tank or Arcade drive option? (Examples below)
-
Can you post your code so we can review it? (Be sure to use ``` [three accent marks in the upper left corner of the keyboard] before and after your code so the VexForum site formats it properly)
Tank
int left = Controller.Axis3.position(vex::percent);
int right = Controller.Axis2.position(vex::percent);
int sideways = Controller.Axis4.position(vex::percent);
//you can also calculate this...
int turn = (right - left) * 0.5;
int forward = (right + left) * 0.5;
frontRight.spin(vex::forward, right - sideways, vex::percent);
frontLeft.spin(vex::forward, left + sideways, vex::percent);
backRight.spin(vex::forward, right + sideways, vex::percent);
backLeft.spin(vex::forward, left - sideways, vex::percent);
Arcade Drive
int forward = Controller.Axis3.position(vex::percent);
int sideways = Controller.Axis4.position(vex::percent);
int turn = Controller.Axis1.position(vex::percent);
frontRight.spin(vex::forward, forward - sideways + turn, vex::percent);
frontLeft.spin(vex::forward, forward + sideways - turn, vex::percent);
backRight.spin(vex::forward, forward + sideways + turn, vex::percent);
backLeft.spin(vex::forward, forward - sideways - turn, vex::percent);
Why do you multiply by -1?
Why there is an infinite do-loop in useControl?
You do not have wait() in the code.
hi here’s the code (I removed comments) Most of the stuff there is forklifts and should not affect the drivetrain (I think)
#include "vex.h"
using namespace vex;
#include "robot-config.h"
competition Competition;
int threshold = 10;
int p = 0;
int liftSpeed = 50;
void precision() {
if (p == 0) {
p = 1;
} else {
p = 0;
}
}
void userControl() {
while (1) {
// Controller1.ButtonA.pressed(precision);
if(Controller1.ButtonUp.pressing() and Controller1.ButtonR1.pressing()){
ForkF.spin(forward, liftSpeed, pct);
waitUntil(!Controller1.ButtonR1.pressing());
ForkF.setVelocity(0, percent);
ForkF.setStopping(brake);
}
if(Controller1.ButtonUp.pressing() and Controller1.ButtonR2.pressing()){
ForkF.spin(reverse, liftSpeed, pct);
waitUntil(!Controller1.ButtonR2.pressing());
ForkF.setVelocity(0, percent);
ForkF.setStopping(brake);
}
if(Controller1.ButtonDown.pressing() and Controller1.ButtonR1.pressing()){
if(Controller1.ButtonDown.pressing()){
ForkB.spin(forward, liftSpeed, pct);
}
waitUntil(!Controller1.ButtonR1.pressing());
ForkB.setVelocity(0, percent);
ForkB.setStopping(brake);
}
if(Controller1.ButtonDown.pressing() and Controller1.ButtonR2.pressing()){
ForkB.spin(reverse, liftSpeed, pct);
waitUntil(!Controller1.ButtonR2.pressing());
ForkB.setVelocity(0, percent);
ForkB.setStopping(brake);
}
if(Controller1.ButtonLeft.pressing() and Controller1.ButtonR1.pressing()){
ForkL.spin(forward, liftSpeed, pct);
waitUntil(!Controller1.ButtonR1.pressing());
ForkL.setVelocity(0, percent);
ForkL.setStopping(brake);
}
if(Controller1.ButtonLeft.pressing() and Controller1.ButtonR2.pressing()){
ForkL.spin(reverse, liftSpeed, pct);
waitUntil(!Controller1.ButtonR2.pressing());
ForkL.setVelocity(0, percent);
ForkL.setStopping(brake);
}
if(Controller1.ButtonRight.pressing() and Controller1.ButtonR1.pressing()){
ForkR.spin(forward, liftSpeed, pct);
waitUntil(!Controller1.ButtonR1.pressing());
ForkR.setVelocity(0, percent);
ForkR.setStopping(brake);
}
if(Controller1.ButtonRight.pressing() and Controller1.ButtonR2.pressing()){
ForkR.spin(reverse, liftSpeed, pct);
waitUntil(!Controller1.ButtonR2.pressing());
ForkR.setVelocity(0, percent);
ForkR.setStopping(brake);
}
int front = Controller1.Axis3.position(vex::percent);
int side = Controller1.Axis4.position(vex::percent);
int turn = Controller1.Axis1.position(vex::percent);
FR.spin(vex::forward, front - side + turn, vex::percent);
FL.spin(vex::forward, front + side - turn, vex::percent);
BR.spin(vex::forward, front + side + turn, vex::percent);
BL.spin(vex::forward, front - side - turn, vex::percent);
}
}
int main() {
vexcodeInit();
Competition.drivercontrol(userControl);
while (true) {
userControl();
}
}
Hi, the first thing is no longer an issue but could you explain what you meant by the other two? Are those needed? It doesn’t seem to affect the movement of the robot.
Since you are adding userControl
to the Competition.drivercontrol
, you don’t need it in the main()
while loop. Also, adding a short wait allows the processor to switch to other tasks.
Below is the default competition template:
int main() {
// Set up callbacks for autonomous and driver control periods.
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol); //<--Already added here.
// Run the pre-autonomous function.
pre_auton();
// Prevent main from exiting with an infinite loop.
while (true) {
//userControl(); <-- Not needed here.
wait(100, msec); //<-- Helps to have a wait here to free up the processor.
}
}
The code looks good from what I can see. The sideways motion is on the left stick (#4). That’s not working?
Hudsonville_Robotics beat me by a few seconds.
usually, the FL should be all plus.
FL.spin(vex::forward, front + side + turn, vex::percent);
front_left = drive + strafe + rotate;
rear_left = drive - strafe + rotate;
front_right = drive - strafe - rotate;
rear_right = drive + strafe - rotate;
Oh, I see. I’ll try that, thank you.
Yep, apparently that’s what’s not working.
I’ll switch that and see if it works, but my current code is the same one as listed in this post, which multiple people say works. I’ll still give it a shot, though. Thank you!
How do the wheels look like? Do they form “X” from the top or “O”?
technik3k discuss this in quite a detail of the theory.
They form an x shape. That thread looks interesting I’ll definitely check it out. Thanks.
Hi, for some reason this code makes it so that axis 3 makes the robot spin and axis 4 to go forwards. Axis 1 also makes the robot go forwards. I have no clue why.
here’s the exact code with your additions:
#include "vex.h"
using namespace vex;
#include "robot-config.h"
competition Competition;
int threshold = 10;
int p = 0;
int liftSpeed = 50;
void precision() {
if (p == 0) {
p = 1;
} else {
p = 0;
}
}
void userControl() {
while (1) {
// Controller1.ButtonA.pressed(precision);
if(Controller1.ButtonUp.pressing() and Controller1.ButtonR1.pressing()){
ForkF.spin(forward, liftSpeed, pct);
waitUntil(!Controller1.ButtonR1.pressing());
ForkF.setVelocity(0, percent);
ForkF.setStopping(brake);
}
if(Controller1.ButtonUp.pressing() and Controller1.ButtonR2.pressing()){
ForkF.spin(reverse, liftSpeed, pct);
waitUntil(!Controller1.ButtonR2.pressing());
ForkF.setVelocity(0, percent);
ForkF.setStopping(brake);
}
if(Controller1.ButtonDown.pressing() and Controller1.ButtonR1.pressing()){
if(Controller1.ButtonDown.pressing()){
ForkB.spin(forward, liftSpeed, pct);
}
waitUntil(!Controller1.ButtonR1.pressing());
ForkB.setVelocity(0, percent);
ForkB.setStopping(brake);
}
if(Controller1.ButtonDown.pressing() and Controller1.ButtonR2.pressing()){
ForkB.spin(reverse, liftSpeed, pct);
waitUntil(!Controller1.ButtonR2.pressing());
ForkB.setVelocity(0, percent);
ForkB.setStopping(brake);
}
if(Controller1.ButtonLeft.pressing() and Controller1.ButtonR1.pressing()){
ForkL.spin(forward, liftSpeed, pct);
waitUntil(!Controller1.ButtonR1.pressing());
ForkL.setVelocity(0, percent);
ForkL.setStopping(brake);
}
if(Controller1.ButtonLeft.pressing() and Controller1.ButtonR2.pressing()){
ForkL.spin(reverse, liftSpeed, pct);
waitUntil(!Controller1.ButtonR2.pressing());
ForkL.setVelocity(0, percent);
ForkL.setStopping(brake);
}
if(Controller1.ButtonRight.pressing() and Controller1.ButtonR1.pressing()){
ForkR.spin(forward, liftSpeed, pct);
waitUntil(!Controller1.ButtonR1.pressing());
ForkR.setVelocity(0, percent);
ForkR.setStopping(brake);
}
if(Controller1.ButtonRight.pressing() and Controller1.ButtonR2.pressing()){
ForkR.spin(reverse, liftSpeed, pct);
waitUntil(!Controller1.ButtonR2.pressing());
ForkR.setVelocity(0, percent);
ForkR.setStopping(brake);
}
int front = Controller1.Axis3.position(vex::percent);
int side = Controller1.Axis4.position(vex::percent);
int turn = Controller1.Axis1.position(vex::percent);
FR.spin(vex::forward, front - side - turn, vex::percent);
FL.spin(vex::forward, front + side + turn, vex::percent);
BR.spin(vex::forward, front + side - turn, vex::percent);
BL.spin(vex::forward, front - side + turn, vex::percent);
}
}
int main() {
Competition.drivercontrol(userControl);
while (true) {
wait(100, msec);
}
}
With all due respect, you don’t “own your code”. What that means is that you are trying to copy / paste existing code and you don’t know how it applies to your robot.
I think you need to analyze the problem so that you understand what you need the wheels to do. Grab your notebook and make a grid of the wheel layout similar to the image that @Codec provided in the original thread.
For the robot to roll forward I need LF, LR, RF, RR to rotate positive.
For the robot to roll backward I need LF, LR, RF, RR to rotate negative.
For the robot to strafe left …
For the robot to strafe right…
For the robot to turn left …
For the robot to turn right…
Once you know which values should be positive and which should be negative, combine that with the values of your controller stick. Down and Left on the sticks are negative values and Up and Right are positive. The stick value is 0 when it is dead center (-100 to 0 to +100) which means the value of a center stick will not affect the other values
When you understand what you need the motors to do you can then map that to the stick values. Use the existing code as a reference. Note that the wheels in the photo follow an X pattern and I believe that your wheels were in an O pattern.
You may need to manually roll / slide your robot and watch how the wheels want to move to determine if they should turn forward or backward.
Send picture of wheels from top and bottom view. Also check your motors polarity. Push axis 3 up, all the wheels should move forward.
Thanks everyone. I didn’t figure out the issue, but for some reason after making the front int negative on BR, the robot works. I have no clue what caused this, but if you have a similar issue I guess try that. Thanks again.