怎样用ArduinoUNO和Wekinator控制伺服电机

电子说

1.2w人已加入

描述

所需硬件

Arduino UNO

2伺服电机

面包板

跳线

必需的软件

Wekinator

Arduino IDE

Wekinator控制的伺服电机电路图

首先连接每个伺服上的红线连接到Arduino的5V引脚。然后将每个伺服的黑线连接到Arduino的地线。最后,将其中一个伺服电机的黄色线连接到Arduino上的引脚8,将黄色线从另一个伺服器连接到引脚9.

通过Wekinator控制伺服系统的电路图

如何在Arduino IDE中运行Wekinator

将本帖末尾提供的Arduino专用代码粘贴到Arduino的IDE中并上传。

然后你需要从Wekinator下载草图文件快速演练页面。

下载演练页面上的屏幕鼠标控制示例。解压缩文件并在处理中运行草图。该草图为Wekinator提供输入。您需要另一个输出部分的草图,您可以在本文末尾找到它。将该代码粘贴到处理窗口并运行它 - 两个处理输出窗口应如下所示:

处理Wekinator中的输出窗口

打开Wekinator并按如下所示更新设置:

将输入窗口设置为2

将输出窗口设置为2

将类型设置为自定义

更新设置后,单击“配置”。

在Wekinator中创建新项目窗口。

单击“配置”后,将打开“自定义输出类型”窗口。在此窗口中,按如下方式调整设置:

在Wekinator中自定义输出类型窗口。

拖动处理中的绿框窗口到左侧中心并将Wekinator窗口中的设置调整为下面显示的值,然后短暂开始和停止录制。

将处理窗口中的绿色框移动到屏幕右侧的中心,并调整Wekinator窗口中的设置,如下所示。再一次,短暂地开始和停止录制

接下来,将处理窗口中的绿框拖到窗口的中央顶部区域并调整设置在下面的Wekinator窗口中显示的那些。再次,快速开始和停止录制。

最后,将处理窗口中的绿色框拖到窗口的底部中心,并将设置调整为反映Wekinator窗口中显示的那些。最后一次,快速开始和停止录制。

单击“训练”按钮,然后选择“运行”。在处理窗口中拖动绿色框时,连接到Arduino的伺服器将相应移动。

处理代码(Wekinator的输出)

import vsync.*; // Importing the library that will help us in sending and receiving the values from the Arduino

import processing.serial.*; // Importing the serial library

// Below libraries will connect and send, receive the values from Wekinator

import oscP5.*;

import netP5.*;

// Creating the instances

OscP5 oscP5;

NetAddress dest;

ValueSender sender;

// These variables will be synchronized with the Arduino and they should be same on the Arduino side.

public int output;

public int output1;

void setup()

{

// Starting the serial communication, the baudrate and the com port should be same as on the Arduino side.

Serial serial = new Serial(this, “COM10”, 19200);

sender = new ValueSender(this, serial);

// Synchronizing the variables as on the Arduino side. The order should be same.

sender.observe(“output”);

sender.observe(“output1”);

// Starting the communication with Wekinator. listen on port 12000, return messages on port 6448

oscP5 = new OscP5(this, 12000);

dest = new NetAddress(“127.0.0.1”, 6448);

}

// Recieve OSC messages from Wekinator

void oscEvent(OscMessage theOscMessage) {

if (theOscMessage.checkAddrPattern(“/wek/outputs”) == true) {

// Receiving the output from Wekinator

float value = theOscMessage.get(0).floatValue(); // First output

float val = theOscMessage.get(1).floatValue(); // Second output

// Converting the output to int type

output = int(value);

output1 = int(val);

}

}

void draw()

{

// Nothing to be drawn for this example

}

Weoulator控制的舵机的Arduino代码

#include // Including the library that will help us in receiving and sending the values from processing

#include // Including the servo library

ValueReceiver《2》 receiver; /*Creating the receiver that will receive up to 2 values.

Put the number of values to synchronize in the brackets */

/* The below two variables will be synchronized in the processing

and they should be same on both sides. */

int output;

int output1;

// Creating the instances

Servo myservo;

Servo myservo1;

void setup()

{

/* Starting the serial communication because we are communicating with the

Arduino through serial. The baudrate should be same as on the processing side. */

Serial.begin(19200);

// Initializing the servo pins

myservo.attach(8);

myservo1.attach(9);

// Synchronizing the variables with the processing. The variables must be int type.

receiver.observe(output);

receiver.observe(output1);

}

void loop()

{

// Receiving the output from the processing.

receiver.sync();

// Check for the info we‘re looking for from Processing

if (output 《 180) {

myservo.write(output);

}

if (output1 《180)

{

myservo1.write(output1);

}

}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分