diff --git a/RC excavator/FW/src/controller.cpp b/RC excavator/FW/src/controller.cpp index cbff5bb..e651ad2 100644 --- a/RC excavator/FW/src/controller.cpp +++ b/RC excavator/FW/src/controller.cpp @@ -4,9 +4,11 @@ XboxSeriesXControllerESP32_asukiaaa::Core xboxController; //"40:8e:2c:41:e4:eb") uint32_t lastcontrollernotify = 0; uint16_t joymax = 0; +uint16_t trgmax = 0; #define deadband 10000 +#define trgdeadband 200 -direction getdirection(int setpoint) +direction getdirectionJoy(int setpoint) { if(setpoint > (joymax/2 +deadband)) { @@ -16,11 +18,35 @@ direction getdirection(int setpoint) { return direction::left; } - else + //else + return direction::stop; +} + +direction getdirecion2Button(uint8_t button0, uint8_t button1) +{ + if(button0 & !button1) { - return direction::stop; + return direction::left; } - + if(!button0 & button1) + { + return direction::right; + } + //else + return direction::stop; +} + +direction getTrackDirection(int trigger, uint8_t shoulder) +{ + if(trigger > (trgmax/2 + trgdeadband) & !shoulder) + { + return direction::right; + } + if((trigger == 0) & shoulder) + { + return direction::left; + } + return direction::stop; } void setup_controller() @@ -47,23 +73,27 @@ void loop_controller() // log_i("message: %s", Serial.print(xboxController.xboxNotif.toString())); unsigned long receivedAt = xboxController.getReceiveNotificationAt(); joymax = XboxControllerNotificationParser::maxJoy; + trgmax = XboxControllerNotificationParser::maxTrig; if (millis() - lastcontrollernotify > CONTROLLERNOTIFY) { - log_i("joyLHori rate: %u", xboxController.xboxNotif.joyLHori); - log_i("joyLVert rate: %u", xboxController.xboxNotif.joyLVert); - log_i("joyRHori rate: %u", xboxController.xboxNotif.joyRHori); - log_i("joyRVert rate: %u", xboxController.xboxNotif.joyRVert); + // log_i("joyLHori rate: %u", xboxController.xboxNotif.btnLS); + // log_i("joyLVert rate: %u", xboxController.xboxNotif.btnLB); + // log_i("joyRHori rate: %u", xboxController.xboxNotif.trigLT); + // log_i("joyRVert rate: %u", xboxController.xboxNotif.trigRT); log_i("battery %d %", xboxController.battery); - log_i("joymax:%d",joymax ); + //log_v("joymax:%d",joymax ); lastcontrollernotify = millis(); } - controlMotor("Pivot", getdirection(xboxController.xboxNotif.joyLHori)); - controlMotor("Dipper", getdirection(xboxController.xboxNotif.joyLVert)); + controlMotor("Pivot", getdirectionJoy(xboxController.xboxNotif.joyLHori)); + controlMotor("Dipper", getdirectionJoy(xboxController.xboxNotif.joyLVert)); + controlMotor("Bucket", getdirectionJoy(xboxController.xboxNotif.joyRHori)); + controlMotor("Boom", getdirectionJoy(xboxController.xboxNotif.joyRVert)); + controlMotor("Push", getdirecion2Button(xboxController.xboxNotif.btnX, xboxController.xboxNotif.btnY)); + controlMotor("Thumb", getdirecion2Button(xboxController.xboxNotif.btnA, xboxController.xboxNotif.btnB)); - controlMotor("Bucket", getdirection(xboxController.xboxNotif.joyRHori)); - controlMotor("Boom", getdirection(xboxController.xboxNotif.joyRVert)); + controlMotor("TrackLeft", getTrackDirection(xboxController.xboxNotif.trigLT, xboxController.xboxNotif.btnLB)); + controlMotor("TrackRight", getTrackDirection(xboxController.xboxNotif.trigRT, xboxController.xboxNotif.btnRB)); - //controlMotor("TrackLeft", getdirection(xboxController.xboxNotif.trigLT) } } diff --git a/RC excavator/FW/src/controller.h b/RC excavator/FW/src/controller.h index 9c0f896..4f56b61 100644 --- a/RC excavator/FW/src/controller.h +++ b/RC excavator/FW/src/controller.h @@ -2,7 +2,7 @@ #include "motors.h" #include -#define CONTROLLERNOTIFY 1000 +#define CONTROLLERNOTIFY 30000 void setup_controller(); diff --git a/RC excavator/FW/src/motors.cpp b/RC excavator/FW/src/motors.cpp index 1c66fb4..2cbd9d8 100644 --- a/RC excavator/FW/src/motors.cpp +++ b/RC excavator/FW/src/motors.cpp @@ -11,14 +11,14 @@ Servo auxServo; motorinit initlist[NUMMOTORS] { - {pivot0, pivot1, 0, "Pivot"}, - {mainBoom0, mainBoom1, 1, "Boom"}, - {secondBoom0, secondBoom1, 2, "Dipper"}, - {tiltAttach0, tiltAttach1, 3, "Bucket"}, - {thumb0, thumb1, 4, "Thumb"}, - {auxAttach0, auxAttach1, 5, "Aux"}, - {leftMotor0, leftMotor1, 6, "TrackLeft"}, - {rightMotor0, rightMotor1, 7, "TrackRight"} + {pivot0, pivot1, 0, "Pivot", true}, + {mainBoom0, mainBoom1, 1, "Boom", false}, + {secondBoom0, secondBoom1, 2, "Dipper", true}, + {tiltAttach0, tiltAttach1, 3, "Bucket", true}, + {thumb0, thumb1, 4, "Thumb", false}, + {auxAttach0, auxAttach1, 5, "Push", false}, + {leftMotor0, leftMotor1, 6, "TrackLeft", true}, + {rightMotor0, rightMotor1, 7, "TrackRight", true} }; std::vector motors; @@ -67,8 +67,17 @@ void motor::begin() void motor::setIO(uint8_t in0, uint8_t in1) { + if(!m_invert) + { mcp.digitalWrite(m_pin0, in0); mcp.digitalWrite(m_pin1, in1); + } + else + { + mcp.digitalWrite(m_pin0, in1); + mcp.digitalWrite(m_pin1, in0); + } + } uint32_t lastmotorlog = 0; diff --git a/RC excavator/FW/src/motors.h b/RC excavator/FW/src/motors.h index 2bf5afe..0d7d2ee 100644 --- a/RC excavator/FW/src/motors.h +++ b/RC excavator/FW/src/motors.h @@ -25,6 +25,7 @@ struct motorinit uint8_t in1; uint8_t id; String name; + bool invert; }; enum direction @@ -47,13 +48,15 @@ class motor String m_name; uint32_t m_lastlog; direction current; + bool m_invert; void setIO(uint8_t in0, uint8_t in1); public: motor(motorinit initstruct): m_pin0(initstruct.in0), m_pin1(initstruct.in1), m_id(initstruct.id), - m_name(initstruct.name) {}; + m_name(initstruct.name), + m_invert(initstruct.invert) {}; void begin(); void run(direction dir);