himawaliss 发表于 2015-9-24 10:07:41

zoologist 发表于 2015-9-24 09:36 static/image/common/back.gif
arduino 板载空间很小的意思,上下位机都是你自己写,就不要搞那么多冗余数据了

上机位是另外一个人,他是用BCI2000(brain computer interface)发信号给我,他现在只能发送给我一堆有用的没用的信号。我想两边都想办法的话可能会快一点。

zoologist 发表于 2015-9-24 11:09:57

himawaliss 发表于 2015-9-24 10:07 static/image/common/back.gif
上机位是另外一个人,他是用BCI2000(brain computer interface)发信号给我,他现在只能发送给我一堆有用 ...

哦 这样的话,你最好让他看一下能否少发一点,否则你这边数据量大也容易死唉

himawaliss 发表于 2015-10-6 16:45:11

zoologist 发表于 2015-9-24 11:09 static/image/common/back.gif
哦 这样的话,你最好让他看一下能否少发一点,否则你这边数据量大也容易死唉

我想问一下,arduino可以同时接收char,int和string么?我在网上查找并没有看到有同时发的例子。我的上几位每次发两行数据给我,一行是字符串一行是整数。我觉得arduino出来后的结果应该都是字符串,因为我一开始定义的时候就将收到的数据定义成了字符串。
1,可以将接收到的数据定义成可字符串可整数么?
2,我需要用接收到的两行写个判断语句,那我是不是要将接收到的数据重新定义成两个数据?然后再写判断?

himawaliss 发表于 2015-10-8 21:09:41

zoologist 发表于 2015-9-24 11:09 static/image/common/back.gif
哦 这样的话,你最好让他看一下能否少发一点,否则你这边数据量大也容易死唉

大神,请问arduino可以读出servo的当前位置么?

zoologist 发表于 2015-10-9 08:32:19

himawaliss 发表于 2015-10-8 21:09 static/image/common/back.gif
大神,请问arduino可以读出servo的当前位置么?

应该无法读出吧?

还需要另外的装置才能反馈

himawaliss 发表于 2015-10-9 09:21:26

zoologist 发表于 2015-10-9 08:32 static/image/common/back.gif
应该无法读出吧?

还需要另外的装置才能反馈

你是说电位器么?我想写一个循环给servo,让servo每收到一个信号就转12°,总共有7个信号。但是我的循环写的有问题。for (int j=7;j>=0;j=j-1){
         if (packetBuffer=='1'&& packetSize<=16){
             int angle1=j*12;   
             myServo.write(angle1);
          myServo1.write(angle1);
          myServo2.write(angle1);
          myServo3.write(angle1);
          myServo4.write(angle1);
          delay(15);
         }
      }所以我想让servo接收到一个信号就读出当前位置,感觉这样比较简单一点。
或者当servo收到第一个1返回1,第二个1返回2...第七个1返回7.然后我再利用这7个数写循环?
可以帮我看一下代码么?谢谢了!
还有一个问题是当我的servo转到0°的时候,就一直抽搐不停的向180°转然后又转到0°。有什么方法可以让servo停在0°不动么?
我在网上查了资料说这跟delay的时间有关但是我试了没有改善。方法如下:
The problem here is to compute TURN_TIME. For this, you must check the datasheet of your servo.

On my own servo, a Feetech Micro 1.3kg Continuous Rotation Servo FS90R, the max speed is:
•0.12s/60° when powered with 4.8V
•0.10s/60° when powered with 6V

However, with the Arduino UNO, the supplied voltage should be exactly 5V, neither 4.8V, nor 6V.

If we take a linear approximation, then we can apply the following formula to find out the speed T (in s/60°):
T = (0.12 - 0.10) * (V - 4.8) / (4.8 - 6.0) + 0.12


Hence, for 5V, we can take it for granted that the max speed should be:
T = (0.12 - 0.10) * (5.0 - 4.8) / (4.8 - 6.0) + 0.12 = 0.116667


Since we need 90°, this means we must run the servo at its max speed during:
T' = T * 1.5 = 0.175s

himawaliss 发表于 2015-10-11 19:15:37

zoologist 发表于 2015-9-23 14:06 static/image/common/back.gif
直接设置一个Port就好。

arduino 设置的这个 port和你程序的要一致

大神,可以请你帮我看一下代码吗?我每次接收一个信号后就不再接收信号了。我觉得我的while loop 写的有问题。#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: [email protected] 12/30/2008
#include <Servo.h>

// Enter a MAC address and IP address of Arduino .
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(169, 254, 148, 239);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving
char packetBuffer; //buffer to hold incoming packet,

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

// define servos
Servo myServo;
Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4;
//servoPin={5,6,7,8,9}

//define control signal
int ControlSignal;
int stopPosition = 90;
int level = 12;

void setup() {
myServo.attach(9);
myServo1.attach(8);
myServo2.attach(7);
myServo3.attach(6);
myServo4.attach(5);
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(9600);
}

void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();

if (packetSize)
{
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    //Serial.print(packetBuffer);
    //Serial.println(packetSize);

    //reset memory
    /* for (int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++){
       packetBuffer=0;
       }
       */

    //decide move direction by targetCode and controlSignal

    int j = 0;
    while (j <= 6) {
      if(packetBuffer == '1' && packetSize <= 16) {
      stopPosition -= level;
      myServo.write(stopPosition);
      myServo1.write(stopPosition);
      myServo2.write(stopPosition);
      myServo3.write(stopPosition);
      myServo4.write(stopPosition);
      delay(15);
      }
      if(packetBuffer == '2' && packetSize > 16) {
      stopPosition = stopPosition + level;
      myServo.write(stopPosition);
      myServo1.write(stopPosition);
      myServo2.write(stopPosition);
      myServo3.write(stopPosition);
      myServo4.write(stopPosition);
      delay(15);
      }
      j++;
    }

    //int k = 0;
    //while (k <= 2) {
    if (packetBuffer == '0') {
      myServo.write(90);
      myServo1.write(90);
      myServo2.write(90);
      myServo3.write(90);
      myServo4.write(90);
      delay(15);
    }
    //k = k + 1;
    //}
}
}




页: 1 [2]
查看完整版本: question of udp communication between arduino and PC via UDP