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!)
#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:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
// initialize serial:
Serial.begin(9600);
stepper.setMaxSpeed(300);
stepper.setAcceleration(100);
// 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();
if(packetSize)
{
// 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);
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
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);
//Serial.println(blue);
stepper.moveTo(red);
// stepper1.moveTo(green);
// }
}
stepper.run();
// stepper1.run();
}