PS4控制器的故障保护,用于控制直流电动机

如何解决PS4控制器的故障保护,用于控制直流电动机

我正在研究通过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,255,255); //Need to figure out how to add offset to remove buzzing 
//noise from motor. 
backwards = map(backwards,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); }


//This is for front l/r Motor

int steer = (PS4.getAnalogHat(LeftHatX) ); 
steer = map(steer,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); }
//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);}

 }}

解决方法

您的代码使用.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

,

如果您无法检测到控制器已断开连接并给出了可以运行的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");
  }
}
,

最后添加了一些东西,以便在控制器已连接且不再连接的情况下,这要感谢一个帮助我的伙伴

/*
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,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,255); //Need to figure out how to add offset to remove buzzing noise from motor. 
backwards = map(backwards,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); }

    
//This is for front l/r Motor

int steer = (PS4.getAnalogHat(LeftHatX) ); 
steer = map(steer,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,LOW);
}
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-