【问题标题】:Fail Safe for PS4 controller used to control dc motors用于控制直流电机的 PS4 控制器的故障保护
【发布时间】:2020-08-12 20:34:32
【问题描述】:

我正在开展一个项目,通过 arduino uno 和 L298N 控制旧遥控汽车直流电机。我有电机工作,但想要包括一个故障保护装置,如果蓝牙超出范围,它会停止电机,这样它就不会继续行驶直到它崩溃。代码在此下方开始,我尝试在最底部进行故障保护。

    //This begins the actual motor stuff 

int forwards = (PS4.getAnalogButton(R2)); // Read R2 Button for forward 
int backwards = (PS4.getAnalogButton(L2)); // Read L2 Button for backwards 
forwards  = map(forwards,  0, 255, 0, 255); //Need to figure out how to add offset to remove buzzing 
//noise from motor. 
backwards = map(backwards, 0, 255, 0, 255);

//This is for Rear motor Forwards
if ((PS4.getAnalogButton( R2 )) > 0 ) {
analogWrite(enA, forwards );
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);}
//This is for Rear motor Backwards 
if  ((PS4.getAnalogButton( L2 )) > 0 ) {
 analogWrite(enA, backwards );
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);} 
//This is so that if neither L2/R2 is pressed the motor does nothing.
if (PS4.getAnalogButton( R2 ) == 0  &&
PS4.getAnalogButton( L2 ) == 0  ){
  analogWrite(enA, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW); }


//This is for front l/r Motor

int steer = (PS4.getAnalogHat(LeftHatX) ); 
steer = map(steer, 0, 255, 255, 255);


//This is for front motor turning left 
//code for analog for l/r 
 if ((PS4.getAnalogHat(LeftHatX)) >135 ){
 analogWrite(enB, steer); 
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);} 
else if ((PS4.getAnalogHat(LeftHatX)) <115 ){
 analogWrite(enB, steer);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH); } 
 else{
  analogWrite(enB, 255);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW); }
//This is code to turn off motor if controller is out of range and disconnect still a wip 
//if (PS4.disconnect())return = true;{      //this comes back as an error "could not convert 
'PS4.PS4BT::<anonymous>.BTHID::disconnect()' from 'void' to 'bool' "
 //analogWrite(enA, 255);
 //digitalWrite(in1, LOW);
 //digitalWrite(in2, LOW);}

 }}

【问题讨论】:

    标签: arduino bluetooth ps4


    【解决方案1】:

    如果您无法检测到控制器已断开连接并给出可以运行的minimal piece of code,那么您的问题会更容易理解。

    话虽如此,正如 Anubhav Nigam 指出的那样,.connected() 是您测试控制器是否仍然连接的方式。

    https://felis.github.io/USB_Host_Shield_2.0/class_p_s4_b_t.html#a08a857b8533e59f6eb872e4c8f727405

    我没有测试这个的硬件,但是看看这个例子:https://www.instructables.com/id/Creating-a-DualShock-4-Controlled-Arduino/

    我相信这是一个最小的可运行示例的样子:

    #include <PS4BT.h>
    #include <usbhub.h>
    
    USB Usb;
    
    BTD Btd(&Usb);
    
    PS4BT PS4(&Btd, PAIR);
    
    void setup() {
      Serial.begin(19200);
      if (Usb.Init() == -1) {
        Serial.print("OSC did not start");
        while (1); // Halt
      }
      Serial.print("PS4 Bluetooth Library Started");
    }
    void loop() {
      Usb.Task();
    
      if (PS4.connected()) {
        if(PS4.getButtonClick(CROSS)){
          Serial.print("cross");
        } 
    
        if(PS4.getButtonClick(PS)){
          PS4.disconnect();
          Serial.end();
        }
          
      } else {   
        Serial.print("switch off motors");
      }
    }
    

    【讨论】:

      【解决方案2】:

      您的代码使用.disconnect() 函数,如果您的设备已连接,它实际上会断开连接,因此它是void 而不是bool。相反,我会使用.connected() 方法,它返回一个bool,告诉您您的设备是否已连接。为了实现这一点,你会这样做:

      if (PS4.connected() == false {
      // do whatever you need to do here to turn off your motors
      }
      

      我希望这会有所帮助,如果您在 Arduino 上需要更多关于蓝牙功能的帮助,可以在这里找到我从中获得函数 .connected() 函数的库:https://www.arduino.cc/en/Reference/CurieBLE

      【讨论】:

      • 我刚刚意识到你也可以使用while循环来实现这个故障保护;你可以把整个程序放在一个大的while循环中,比如while (PS4.connected()) {do the program}
      • 我认为这两个都会导致它出错
      • 不,但我最终确实找到了一些东西,添加了->bool Paired=false;在全局变量下,-> if (PS4.connected()) { Paired=true;然后 else if(Paired==true){//这意味着过去已连接但不再连接。 //停止所有电机模拟写入(ENA,255);数字写入(in1,低);数字写入(in2,低);
      【解决方案3】:

      最后添加了一些东西,如果控制器已连接但不再连接,感谢帮助我的好友

      /*
      PS4 Bluetooth Controlled RC Car- developed by David Duronslet, 
      Code adapted from example sketch for the PS4 Bluetooth library by Kristian Lauszus
       */
      #include <PS4BT.h>
      #include <usbhub.h>
      #ifdef dobogusinclude
      #include <spi4teensy3.h>
      #endif
      #include <SPI.h>
      
      
      ////////////GLOBALS DEFINED HERE///////////
      
      bool Paired=false; 
      //For motor 1 
      #define enA 9
      #define in1 4
      #define in2 5
      
      //For motor 2 
      #define in3 6
      #define in4 7
      #define enB 10 
      
      //For PS4 Controller inputs on debug menu 
      #define ENABLE_UHS_DEBUGGING 1
      
      USB Usb; //use Usb class
      BTD Btd(&Usb);
      bool printAngle, printTouch;
      uint8_t oldL2Value, oldR2Value;
      
      int val = map(PS4.getAnalogButton(R2), 255, 100, 100, 255)   ; //map function for R2 button 
      
      void setup() {
        Serial.begin(115200);//communication baud rate for the Serial port.
        
      #if !defined(__MIPSEL__)
        while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
      #endif
        if (Usb.Init() == -1) {
          Serial.print(F("\r\nOSC did not start"));
          while (1); // Permanently Halt
        }
        Serial.print(F("\r\nPS4 Bluetooth Library Started "));
      
        //Motor Controller Pin Setup
        pinMode(enA, OUTPUT);
        pinMode(enB, OUTPUT);
        pinMode(in1, OUTPUT);
        pinMode(in2, OUTPUT);
        pinMode(in3, OUTPUT);
        pinMode(in4, OUTPUT);
      }
      
      void loop() {
        Usb.Task();
       if (PS4.connected()) {
        Paired=true; //for the range checker 
      //This begins the actual motor stuff 
      
      int forwards = (PS4.getAnalogButton(R2)); // Read R2 Button for forward 
      int backwards = (PS4.getAnalogButton(L2)); // Read L2 Button for backwards 
      forwards  = map(forwards,  0, 255, 0, 255); //Need to figure out how to add offset to remove buzzing noise from motor. 
      backwards = map(backwards, 0, 255, 0, 255);
      
      //This is for Rear motor Forwards
        if ((PS4.getAnalogButton( R2 )) > 0 ) {
          analogWrite(enA, forwards );
          digitalWrite(in1, HIGH);
          digitalWrite(in2, LOW);}
      //This is for Rear motor Backwards 
      if  ((PS4.getAnalogButton( L2 )) > 0 ) {
           analogWrite(enA, backwards );
          digitalWrite(in1, LOW);
          digitalWrite(in2, HIGH);} 
      //This is so that if neither L2/R2 is pressed the motor does nothing.
      if (PS4.getAnalogButton( R2 ) == 0  &&
          PS4.getAnalogButton( L2 ) == 0  ){
            analogWrite(enA, 255);
          digitalWrite(in1, LOW);
          digitalWrite(in2, LOW); }
      
          
      //This is for front l/r Motor
      
      int steer = (PS4.getAnalogHat(LeftHatX) ); 
      steer = map(steer, 0, 255, 255, 255);
      
      
      //This is for front motor turning left 
      //code for analog for l/r 
       if ((PS4.getAnalogHat(LeftHatX)) >135 ){
       analogWrite(enB, steer); 
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH);} 
      else if ((PS4.getAnalogHat(LeftHatX)) <115 ){
       analogWrite(enB, steer);
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW); } 
       else{
            analogWrite(enB, steer);//Change 255 value?
          digitalWrite(in3, LOW);
          digitalWrite(in4, LOW); }
      
       }
      
      //Out of range condition goes here! 
      else if(Paired==true){//this means the was Connected in the past but is no longer connected.
        //stop all motors
          analogWrite(enA, 255);
          digitalWrite(in1, LOW);
          digitalWrite(in2, LOW);
      }
       }
      

      【讨论】:

        猜你喜欢
        • 2018-02-20
        • 2016-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-23
        • 1970-01-01
        • 2021-06-30
        • 1970-01-01
        相关资源
        最近更新 更多