- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对解决方案有疑问,但我不理解该解决方案。它可能类似于 Arduino: initialise custom object in constructor但我认为这不是同一个问题。
上下文如下。我正在用 Arduino 板控制一个小腿机器人。每条腿都是一个包含两个 Helm 机的对象“MChLeg”。使用库控制伺服系统。
问题如下:如果我在构造函数中分配 Helm 机,程序编译正常但 Helm 机行为异常(我想我破坏了 Helm 机调度);如果我在构造函数之外分配 Helm 机,一切都很好。
下面我只列出了 C++ 类的一部分,它是唯一在工作版本和非工作版本之间发生变化的部分。
初始程序(在构造函数中分配伺服),代码有问题:
#include <Arduino.h>
#include <Servo.h>
class MChLeg{
int kneeIndex, hipIndex;
int kneeAngle, hipAngle;
Servo kneeServo;
Servo hipServo;
public:
MChLeg(int, int); // (knee servo index, hip servo index); use 0 if no servo
~MChLeg();
void setLeg(int, int); // (knee angle, hip angle); in degrees, 0-180, 90 is straight
void rollLeg();
};
MChLeg::MChLeg(int x, int y){
this->kneeIndex = x;
this->hipIndex = y;
if (this->kneeIndex != 0) {
this->kneeServo.attach(this->kneeIndex);
this->kneeServo.write(90); // this line can be removed with no impact on issue
}
if (this->hipIndex != 0) {
this->hipServo.attach(this->hipIndex);
this->hipServo.write(90); // this line can be removed with no impact on issue
}
}
新代码(使用方法分配伺服),代码运行良好:
#include <Arduino.h>
#include <Servo.h>
class MChLeg{
int kneeIndex, hipIndex;
int kneeAngle, hipAngle;
Servo kneeServo;
Servo hipServo;
public:
MChLeg();
~MChLeg();
void assignLeg(int, int); // (knee servo index, hip servo index); use 0 if no servo
void setLeg(int, int); // (knee angle, hip angle); in degrees, 0-180, 90 is straight
void rollLeg();
};
MChLeg::MChLeg(){
this->kneeIndex = 0;
this->hipIndex = 0;
}
void MChLeg::assignLeg(int x, int y){
if (this->kneeIndex != 0) {
this->kneeServo.detach();
}
if (this->hipIndex != 0) {
this->hipServo.detach();
}
this->kneeIndex = x;
this->hipIndex = y;
if (this->kneeIndex != 0) {
this->kneeServo.attach(this->kneeIndex);
}
if (this->hipIndex != 0) {
this->hipServo.attach(this->hipIndex);
}
}
为什么第二个代码会比第一个更好?
调用代码:
// Include application, user and local libraries
#include <Servo.h>
#include "MChButton.h"
#include "MChLeg.h"
// pin connections
const int buttonIndex = 2; //pin for button
const int greenLed = 3;//pin for green Led
const int yellowLed = 4;//pin for yellow Led
const int redLed = 5;//pin for red Led
const int servoPin1 = 9;//pin for servo
const int servoPin2 = 10;//pin for servo
const int servoPin3 = 11;//pin for servo
//define persitent objects
MChButton theButton(buttonIndex); // declare the switch pin as an input
MChLeg rightLeg; // declare right leg - was 'MChLeg rightLeg(0, servoPin1);' in first version
MChLeg leftLeg; // declare left leg - was 'MChLeg leftLeg(servoPin2, servoPin3);' in first version
// Setup phase
void setup(){
// declare the LED pins as outputs
pinMode(greenLed,OUTPUT);
pinMode(yellowLed,OUTPUT);
pinMode(redLed,OUTPUT);
leftLeg.assignLeg(0, servoPin1); //right knee - hip - did not exist in first version
rightLeg.assignLeg(servoPin2, servoPin3); //right knee - hip - did not exist in first version
}
// Loop phase
void loop(){
//state machine counter (persistant)
static int machinestate = 0;
const int machinestateMax = 4; //max statemachine
const int machinestateMax1 = machinestateMax+1; //max statemachine for modulo counting
//check if there is an event, if there is one increment state machine counter and mark event done
//statemachine counter is limited for machinestateMac (modulus)
if (theButton.buttonEventGet()==true) {
machinestate = ++machinestate % machinestateMax1;
}
// set LEDs and Legs according to state
switch (machinestate) {
case 0:{
digitalWrite(greenLed, HIGH); // turn the green LED on pin 3 on
digitalWrite(yellowLed, LOW); // turn the red LED on pin 4 off
digitalWrite(redLed, LOW); // turn the red LED on pin 5 off
leftLeg.setLeg(10, 10); // set leg to 0°
rightLeg.setLeg(10, 10); //set leg to 0°
break;
}
case 1:{
digitalWrite(greenLed, HIGH);
digitalWrite(yellowLed, HIGH);
digitalWrite(redLed, LOW);
leftLeg.setLeg(45, 45);
rightLeg.setLeg(45, 45);
break;
}
default:{ // detect if I did it wrong :-)
digitalWrite(greenLed, HIGH);
digitalWrite(yellowLed, HIGH);
digitalWrite(redLed, HIGH);
}
}
delay(10);
}
感谢您的帮助。
最佳答案
我现在没有带 Arduino,但我记得遇到过同样的问题。
据我所知,我在我的构造函数中使用了一个 for 循环
来处理一个无效的数组。所以我最后只是一个一个地赋值。
如果使用 if 语句
,我假设您遇到的问题。
今晚晚些时候回到家后,我会尝试不同的事情。
编辑 - 2015/01/08
我做了一些测试试图重现我所说的问题,但没有成功。我会保留我的答案作为解决方案可能。
关于c++ - Arduino:如果在构造函数中使用 this->object.attach(pin),Servo 类会破坏伺服行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27769400/
尝试使用集成到 QTCreator 的表单编辑器,但即使我将插件放入 QtCreator.app/Contents/MacOS/designer 也不会显示。不过,相同的 dylib 文件确实适用于独
在此代码示例中。 “this.method2();”之后会读到什么?在返回returnedValue之前会跳转到method2()吗? public int method1(int returnedV
我的项目有通过gradle配置的依赖项。我想添加以下依赖项: compile group: 'org.restlet.jse', name: 'org.restlet.ext.apispark', v
我将把我们基于 Windows 的客户管理软件移植到基于 Web 的软件。我发现 polymer 可能是一种选择。 但是,对于我们的使用,我们找不到 polymer 组件具有表格 View 、下拉菜单
我的项目文件夹 Project 中有一个文件夹,比如 ED 文件夹,当我在 Eclipse 中指定在哪里查找我写入的文件时 File file = new File("ED/text.txt"); e
这是奇怪的事情,这个有效: $('#box').css({"backgroundPosition": "0px 250px"}); 但这不起作用,它只是不改变位置: $('#box').animate
这个问题在这里已经有了答案: Why does OR 0 round numbers in Javascript? (3 个答案) 关闭 5 年前。 Mozilla JavaScript Guide
这个问题在这里已经有了答案: Is the function strcmpi in the C standard libary of ISO? (3 个答案) 关闭 8 年前。 我有一个问题,为什么
我目前使用的是共享主机方案,我不确定它使用的是哪个版本的 MySQL,但它似乎不支持 DATETIMEOFFSET 类型。 是否存在支持 DATETIMEOFFSET 的 MySQL 版本?或者有计划
研究 Seam 3,我发现 Seam Solder 允许将 @Named 注释应用于包 - 在这种情况下,该包中的所有 bean 都将自动命名,就好像它们符合条件一样@Named 他们自己。我没有看到
我知道 .append 偶尔会增加数组的容量并形成数组的新副本,但 .removeLast 会逆转这种情况并减少容量通过复制到一个新的更小的数组来改变数组? 最佳答案 否(或者至少如果是,则它是一个错
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
noexcept 函数说明符是否旨在 boost 性能,因为生成的对象中可能没有记录异常的代码,因此应尽可能将其添加到函数声明和定义中?我首先想到了可调用对象的包装器,其中 noexcept 可能会产
我正在使用 Angularjs 1.3.7,刚刚发现 Promise.all 在成功响应后不会更新 angularjs View ,而 $q.all 会。由于 Promises 包含在 native
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我正在编写一个玩具(物理)矢量库,并且遇到了 GHC 坚持认为函数应该具有 Integer 的问题。是他们的类型。我希望向量乘以向量以及标量(仅使用 * ),虽然这可以通过仅使用 Vector 来实现
PHP 的 mail() 函数发送邮件正常,但 Swiftmailer 的 Swift_MailTransport 不起作用! 这有效: mail('user@example.com', 'test
我尝试通过 php 脚本转储我的数据,但没有命令行。所以我用 this script 创建了我的 .sql 文件然后我尝试使用我的脚本: $link = mysql_connect($host, $u
使用 python 2.6.4 中的 sqlite3 标准库,以下查询在 sqlite3 命令行上运行良好: select segmentid, node_t, start, number,title
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我是一名优秀的程序员,十分优秀!