RobotHardware-UESTC/Hardware/银星机器人底盘/PiRobot-YH_Firmware v1.0/STM32/Lib/Joystick/joystick.cpp

269 lines
5.5 KiB
C++
Raw Permalink Normal View History

2024-01-20 13:19:09 +08:00
#include "joystick.h"
#include "board.h"
#include "print.h"
#define pressures false
#define rumble false
Joystick::Joystick()
{
}
bool Joystick::init()
{
2024-01-23 15:06:39 +08:00
log("init joystick");
2024-01-20 13:19:09 +08:00
//PS2_SetInit();
#if 0
2024-01-23 15:06:39 +08:00
unsigned long start = Board::get()->getTickCount();
while(Board::get()->getTickCount()-start>500){
2024-01-20 13:19:09 +08:00
;
}
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if (error == 0)
{
2024-01-23 15:06:39 +08:00
log("Teleop start");
2024-01-20 13:19:09 +08:00
return true;
}
if (error == 1)
2024-01-23 15:06:39 +08:00
log("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
2024-01-20 13:19:09 +08:00
else if (error == 2)
2024-01-23 15:06:39 +08:00
log("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
2024-01-20 13:19:09 +08:00
else if (error == 3)
2024-01-23 15:06:39 +08:00
log("Controller refusing to enter Pressures mode, may not support it. ");
2024-01-20 13:19:09 +08:00
else
2024-01-23 15:06:39 +08:00
log("Teleop err");
2024-01-20 13:19:09 +08:00
#endif
return false;
}
#define JOYSTICK_TEST
void Joystick::test()
{
#ifdef JOYSTICK_TEST
2024-01-23 15:06:39 +08:00
unsigned char key = PS2_DataKey();
2024-01-20 13:19:09 +08:00
//有按键按下
if(key!=0) {
switch (key) {
case PSB_SELECT:
2024-01-23 15:06:39 +08:00
log("SELECT");
2024-01-20 13:19:09 +08:00
break;
case PSB_L3:
2024-01-23 15:06:39 +08:00
log("L3");
2024-01-20 13:19:09 +08:00
break;
case PSB_R3:
2024-01-23 15:06:39 +08:00
log("R3");
2024-01-20 13:19:09 +08:00
break;
case PSB_START:
2024-01-23 15:06:39 +08:00
log("START");
2024-01-20 13:19:09 +08:00
break;
case PSB_PAD_UP:
2024-01-23 15:06:39 +08:00
log("UP");
2024-01-20 13:19:09 +08:00
break;
case PSB_PAD_RIGHT:
2024-01-23 15:06:39 +08:00
log("RIGHT");
2024-01-20 13:19:09 +08:00
break;
case PSB_PAD_DOWN:
2024-01-23 15:06:39 +08:00
log("DOWN");
2024-01-20 13:19:09 +08:00
break;
case PSB_PAD_LEFT:
2024-01-23 15:06:39 +08:00
log("LEFT");
2024-01-20 13:19:09 +08:00
break;
case PSB_L2:
2024-01-23 15:06:39 +08:00
log("L2");
2024-01-20 13:19:09 +08:00
break;
case PSB_R2:
2024-01-23 15:06:39 +08:00
log("R2");
2024-01-20 13:19:09 +08:00
break;
case PSB_L1:
2024-01-23 15:06:39 +08:00
log("L1");
2024-01-20 13:19:09 +08:00
break;
case PSB_R1:
2024-01-23 15:06:39 +08:00
log("R1");
2024-01-20 13:19:09 +08:00
break;
case PSB_TRIANGLE:
2024-01-23 15:06:39 +08:00
log("TRIANGLE");
2024-01-20 13:19:09 +08:00
break;
case PSB_CIRCLE:
2024-01-23 15:06:39 +08:00
log("CIRCLE");
2024-01-20 13:19:09 +08:00
break;
case PSB_CROSS:
2024-01-23 15:06:39 +08:00
log("CROSS");
2024-01-20 13:19:09 +08:00
break;
case PSB_SQUARE:
2024-01-23 15:06:39 +08:00
log("SQUARE");
2024-01-20 13:19:09 +08:00
break;
default:
2024-01-23 15:06:39 +08:00
log("UNKNOWN");
2024-01-20 13:19:09 +08:00
break;
}
}
2024-01-23 15:06:39 +08:00
log("(need to switch mode) %5d %5d %5d %5d",PS2_AnologData(PSS_LX),PS2_AnologData(PSS_LY), PS2_AnologData(PSS_RX),PS2_AnologData(PSS_RY) );
2024-01-20 13:19:09 +08:00
#endif
}
bool Joystick::check(short& liner_x, short liner_y, short& angular_z){
#if JOYSTICK_FOR_HOLONOMIC
return holonomic_check(liner_x, liner_y, angular_z);
#else
return nonholonomic_check(liner_x, liner_y, angular_z);
#endif
}
bool Joystick::holonomic_check(short& liner_x, short liner_y, short& angular_z){
bool rtn = false;
2024-01-23 15:06:39 +08:00
unsigned char key = PS2_DataKey();
2024-01-20 13:19:09 +08:00
liner_y = liner_y;
//up down left right for liner x, y
if (key == PSB_PAD_UP) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("UP");
2024-01-20 13:19:09 +08:00
#endif
liner_x = MAX_LINER_X;
rtn = true;
}
if(key == PSB_PAD_DOWN) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("DOWN");
2024-01-20 13:19:09 +08:00
#endif
liner_x = -MAX_LINER_X;
rtn = true;
}
if(key == PSB_PAD_RIGHT) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("RIGHT");
2024-01-20 13:19:09 +08:00
#endif
liner_y = MAX_LINER_Y;
rtn = true;
}
if(key == PSB_PAD_LEFT) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("LEFT");
2024-01-20 13:19:09 +08:00
#endif
liner_y = -MAX_LINER_Y;
rtn = true;
}
//triangle square circle cross for angular z
if (key == PSB_SQUARE) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("SQUARE");
2024-01-20 13:19:09 +08:00
#endif
angular_z = MAX_ANGULAR_Z;
rtn = true;
}
if(key == PSB_CIRCLE) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("CIRCLE");
2024-01-20 13:19:09 +08:00
#endif
angular_z = -MAX_ANGULAR_Z;
rtn = true;
}
if (key == PSB_L1) { //print stick values if either is TRUE
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("Stick Values:");
log("%d", PS2_AnologData(PSS_LY));
log(",");
log("%d", PS2_AnologData(PSS_LX));
log(",");
log("%d", PS2_AnologData(PSS_RY));
log(",");
log("%d", PS2_AnologData(PSS_RX));
2024-01-20 13:19:09 +08:00
#endif
if (PS2_AnologData(PSS_LX) == 255 && PS2_AnologData(PSS_LX) == 255 &&
PS2_AnologData(PSS_LX) == 255 && PS2_AnologData(PSS_LX) == 255) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("switch mode for use rocker");
2024-01-20 13:19:09 +08:00
#endif
} else {
2024-01-23 15:06:39 +08:00
liner_x = ((255.0/2) - PS2_AnologData(PSS_LY)) / (255.0/2) * MAX_LINER_X;
liner_y = ((255.0/2) - PS2_AnologData(PSS_LX)) / (255.0/2) * MAX_LINER_Y;
angular_z = ((255.0/2) - PS2_AnologData(PSS_RX)) / (255.0/2) * MAX_ANGULAR_Z;
2024-01-20 13:19:09 +08:00
rtn = true;
}
}
return rtn;
}
bool Joystick::nonholonomic_check(short& liner_x, short liner_y, short& angular_z){
liner_y = 0;
bool rtn = false;
unsigned char key=PS2_DataKey();
liner_y = liner_y;
//will be TRUE if button was JUST pressed
if (key == PSB_PAD_UP) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("Up held this hard: ");
2024-01-20 13:19:09 +08:00
#endif
liner_x = MAX_LINER_X;
rtn = true;
}
if(key == PSB_PAD_RIGHT) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("Right held this hard: ");
2024-01-20 13:19:09 +08:00
#endif
angular_z = MAX_ANGULAR_Z;
rtn = true;
}
if(key == PSB_PAD_LEFT) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("LEFT held this hard: ");
2024-01-20 13:19:09 +08:00
#endif
angular_z = -MAX_ANGULAR_Z;
rtn = true;
}
if(key == PSB_PAD_DOWN) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("DOWN held this hard: ");
2024-01-20 13:19:09 +08:00
#endif
liner_x = -MAX_LINER_X;
rtn = true;
}
if (key == PSB_L1) { //print stick values if either is TRUE
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("Stick Values:");
log("%d", PS2_AnologData(PSS_LY));
log(",");
log("%d", PS2_AnologData(PSS_LX));
log(",");
log("%d", PS2_AnologData(PSS_RY));
log(",");
log("%d", PS2_AnologData(PSS_RX));
2024-01-20 13:19:09 +08:00
#endif
if (PS2_AnologData(PSS_LX) == 255 && PS2_AnologData(PSS_LX) == 255 &&
PS2_AnologData(PSS_LX) == 255 && PS2_AnologData(PSS_LX) == 255) {
#if JOYSTICK_DEBUG_ENABLE
2024-01-23 15:06:39 +08:00
log("switch mode for use rocker");
2024-01-20 13:19:09 +08:00
#endif
} else {
liner_x = ((255.0/2)-PS2_AnologData(PSS_LY))/(255.0/2)*MAX_LINER_X;
angular_z = ((255.0/2)-PS2_AnologData(PSS_LX))/(255.0/2)*MAX_ANGULAR_Z;
rtn = true;
}
}
return rtn;
}