0

托尼在这里,我对整个 arduino 世界来说是一个全新的人。

首先是我的项目,我的设备,为什么我决定使用 arduino,我正在尝试做什么,以及我正在使用的代码。

我的项目比较简单;能够平移和倾斜 2 个重量为 0.06 盎司的摄像机。摄像机被用来记录天气。下面的编程有效,但我遇到的问题是,当我释放操纵杆时,舵机返回零(或者我们称之为 90° 位置)。我使用的操纵杆与 Playstation 2 游戏手柄中使用的类型相同。它们允许使用 Z 轴,对我来说,我想用它来保持相机指向我上次定位的方向,直到我希望将它平移或倾斜到不同的轴。我希望我解释清楚。

最后,我不想使用无线,我希望使用硬接线,因为我相信它会减少我所在地区其他无线电频率的干扰。

我的设备或组件如下:

 Arduino UNO board

 Bread board

 Circuit board

 2 Joysticks (Playstation 2 style with X, Y and Z axis)

 4 TowerPro servo motors SG5010

我决定使用 arduino,因为它是计算机/电子商店人员向我推荐的。我没有编码 arduino 或任何其他类型的编程的先验知识。然而,我能够为 1 个操纵杆和 2 个伺服器进行编码,并将其扩展为包括一个附加的操纵杆和 2 个附加的伺服器。如前所述,我有 4 个伺服器和 2 个操纵杆的编码,我不能也不明白我需要做什么才能使 Z 轴能够执行我上面所说的操作。

我向那些更有经验的人道歉,我把这个带到这里,但我真的需要帮助。

编码:

#include <Servo.h>

const int servo1 = 3;        // first servo
const int servo2 = 11;      // second servo
const int servo3 = 6;        // third servo
const int servo4 = 10;     // fourth servo
const int joyH = 3;           // L/R Parallax Thumbstick
const int joyV = 4;           // U/D Parallax Thumbstick
const int joyI = 1;             // L/R Parallax Thumbstick
const int joyW = 2;          // U/D Parallax Thumbstick
int servoVal;           // variable to read the value from the analog pin

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
Servo myservo3;  // create servo object to control a servo
Servo myservo4;  // create servo object to control a servo

void setup() {
    // Servo  
    myservo1.attach(servo1);  // attaches the servo
    myservo2.attach(servo2);  // attaches the servo
    myservo3.attach(servo3);  // attaches the servo
    myservo4.attach(servo4);  // attaches the servo

    // Inizialize Serial
    Serial.begin(9600);
}

void loop(){
    // Display Joystick values using the serial monitor
    outputJoystick();

    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyH);

    servoVal = map(servoVal, 0, 1023, 0, 179);  // scale it to use it with the servo (result  between 0 and 179 to keep from being jittery)

    myservo2.write(servoVal);                  // sets the servo position according to the scaled value    

    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyV);           
    servoVal = map(servoVal, 0, 1023, 0, 179);      // scale it to use it with the servo (result  between 0 and 179 to keep from being jittery)
    myservo4.write(servoVal);                           // sets the servo position according to the scaled value

    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyI);           
    servoVal = map(servoVal, 0, 1023, 0, 179);      // scale it to use it with the servo (result  between 0 and 179 to keep from being jittery)
    myservo1.write(servoVal);                           // sets the servo position according to the scaled value

    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyW);           
    servoVal = map(servoVal, 0, 1023, 0, 179);      // scale it to use it with the servo (result  between 0 and 179 to keep from being jittery)

    myservo3.write(servoVal);                           // sets the servo position according to the scaled value

    delay(15);                                       // waits for the servo to get there
}

/**
 * Display joystick values
 */

void outputJoystick(){
    Serial.print(analogRead(joyH));
    Serial.print ("---"); 
    Serial.print(analogRead(joyV));
    Serial.println ("----------------");
    Serial.print(analogRead(joyI));
    Serial.print ("---"); 
    Serial.print(analogRead(joyW));
    Serial.println ("----------------");
}

感谢您花时间帮助我。我很感激。

4

2 回答 2

0

对于初学者,我对 Arduino 并不熟悉,但是.. 如果您希望在操纵杆处于中间位置时相机不移动,那么您需要在循环中添加一些逻辑。您可以将 LOOP 中的所有内容包装在 IF 语句中 - 如果(操纵杆不在中性位置)然后 {运行您的代码}。这是一个快速肮脏的解决方案,可能不是首选方法,但它应该可以工作。祝你好运!

于 2013-04-30T14:32:07.690 回答
0

It sounds to me like the easiest solution is to modify the servos to be continuos rotation (remove the internal servo pot). This will make the servo act the way you want if i'm understanding you right?

You want to move the joystick a little bit and the cameras will start tracking in that direction slowly, and then when you move the joystick more in that direction the camera tracks even faster, and then when you let the joystick to return to the middle, you want the cameras to stop where they are yeah?

If so, we're talking continuos servo rotation behaviour.. This can be simulated using a small device that plugs between the servo and the servo signal. I'd actually love to find the code to make my own version..

Sorry I can't be any more help than that

Cheers

Andy

于 2014-01-14T18:18:19.530 回答