Go Down

Topic: stepper control over tcp(command packet strategy/parsing commands from UDP) (Read 373 times) previous topic - next topic


Successfully controlling a stepper over ethernet by sending my probably sloppy, bloated code. Now I'm wanting to add an occasional command to change acceleration and speed limit.
 I like how efficient things are running now and am assuming i don't want to bloat my command with constant calls to change accel  and speed on every loop (but maybe I'm worrying too much ? - only  will find out by trying , yep.).
But anyhoo thought I might want to send a message like " S1000, A500" occasionally and parse out 'speed 1000', 'Accel500'.
I'm having trouble with the parse part of things and requesting some help. Also if you can help me make my code pretty(er) be obliged.

SO basically I currently send absolute position via UDP, wish to develop this into position, accel/speed parameters(occasionally), reset origin(occasionally), etc.
How would you proceed, in order to maintain as close to realtime performance as possible ?
Response from this rudimentary patch is peppy(wireless-to-ethernet even ). And I want to keep it that way.

Is UDP apropos for this in the first place ? Seems to be working well though it doesn't have error correction- it has speed!)

Code: [Select]
#include <AccelStepper.h>
#include <MultiStepper.h>

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

#include <AccelStepper.h>
AccelStepper stepper(AccelStepper::DRIVER, 4,5);
//AccelStepper stepper1(AccelStepper::DRIVER, 6, 7);

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,1,10,55); 
int red = 0;
unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

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

void setup() {
    // start the Ethernet and UDP:
  // initialize serial:
//  stepper1.setMaxSpeed(5000);
//  stepper1.setAcceleration(1000);


void loop() {
//  // if there's any serial available, read it:
//  if (Serial.available() > 0) {
//    // look for the next valid integer in the incoming serial stream:
//    int red = Serial.parseInt();
//    // do it again:
//    int green = Serial.parseInt();
//    // do it again:
//    //int blue = Serial.parseInt();
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
//    Serial.print("Received packet of size ");
//    Serial.println(packetSize);
//    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
//      Serial.print(remote[i], DEC);
      if (i < 3)
//        Serial.print(".");
//    Serial.print(", port ");
//    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer

//    Serial.println("Contents:");
//    Serial.println(packetBuffer);
    red = atol(packetBuffer);
    for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer[i] = 0;
    // look for the newline. That's the end of your
    // sentence:
//    if (Serial.read() == '\n') {

      // print the three numbers in one string as hexadecimal:
//      Serial.print(red);
//      Serial.print(green);
//      stepper1.moveTo(green);

//    }
//        stepper1.run();


I'm having trouble with the parse part of things
It would be an order of magnitude (or more) easier if you sent "S, 1000, A, 500".

It would be a lot easier to help you if you told us what the trouble is.

Before posting code, please remove all the commented out code, unless the reason for posting is to discuss the commented out code.

Code: [Select]
    red = atol(packetBuffer);

packetBuffer is NOT a string. You should NOT be passing something that is not a string to a function that expects a string. You SHOULD be aware of the differences between a char array and a string (that pesky NULL terminator does so matter).

It is one line of code to make packetBuffer a string, so that you CAN pass it to functions that expect strings, like atol() or strtok().

Is UDP apropos for this in the first place ?
Not in my opinion. UDP packets follow the USPS service model, not the UPS service model.

UPS guarantees to deliver your package. USPS guarantees to try to deliver your package.

If I were trying to get data from point A to point B, I'd actually expect the data to get there. If I didn't care whether it got there, or not, I wouldn't be sending it.

UDP is great for cases where the occasional packet can be lost, without significant adverse affects, such as streaming voice over IP. If the occasional 2 milliseconds of voice data doesn't make it, you probably wouldn't even notice.

TCP tries a lot harder to make sure the packets are sent, in the proper order.
The art of getting good answers lies in asking good questions.

Go Up