Robin2:
Not without seeing your code.
Please find a code below. As advised previously, by this code i can move forvard and “stop” (not realy stop but set sped to 1" both motors. I was trying to replace shutdownAllMotors() with something which makes motors go backward…and no reaction.
#include <SPI.h>
#include <Ethernet.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMSmove(0x60); // Default address, no jumpers
Adafruit_StepperMotor *myStepper1 = AFMSmove.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMSmove.getStepper(200, 2);
void forwardstep1() {
myStepper1->onestep(BACKWARD, DOUBLE);
}
void forwardstep2() {
myStepper2->onestep(FORWARD, DOUBLE);
}
AccelStepper stepper1(forwardstep1, forwardstep1);
AccelStepper stepper2(forwardstep2, forwardstep2);
// Number of pins that motors are conneted to (length of the array below)
// The #define lets me staticially allocate the array below for the
// structures.
const int kNumberOfMotors = 4;
// Minimum and maximum duty cycles for the motors. I determined these
// by experimentation, and they may need to be tweaked for other motors.
const int kMinPwmDutyCycle = 75;
const int kMaxPwmDutyCycle = 224;
// These are for values entered over the TCP interface. They are mapped
// to the PwmDutyCycle values above by convertSpeedToPwmDutyCycle().
const int kMinSpeed = 0;
const int kMaxSpeed = 100.0;
// Number of milliseconds to operate motors without a command
// on the TCP server. If no commands are reeived after this timeout,
// all motors will shut down.
const unsigned long kShutdownTimeoutMillis = 60000;
unsigned long timestampOfLastCommand;
/*** END motor operation variables ***/
/*** BEGIN network customization ***/
const int serverPort = 17;
EthernetServer server(serverPort);
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xD7, 0xD2 };
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress ip(192, 168, 0, 33);
/*** END network customization ***/
/*** BEGIN Buffer declaration ***/
// These are buffer declarations to hold strings for TCP/IP input and output
//
// statusBuffer holds status messages to be written to TCP/IP
//
// tcpReadBuffer holds data as it is received bt TCP/IP
//
// tcpShouldDisconnect is true if the TCP/IP interface should disconnect. Not
// a buffer, but does deal with its management
//
// All the text handling is through C strings.
#define BUFFER_SIZE 256
char statusBuffer[BUFFER_SIZE];
char tcpReadBuffer[BUFFER_SIZE];
bool tcpShouldDisconnect;
/*** END Buffer declaration ***/
/*** BEGIN Main setup and loop functions ***/
// setup() calls all the subsystem setup functions.
void setup()
{
AFMSmove.begin(); // Start the move shield
Serial.begin(9600);
Serial.println("ProtocolRunner: Hello! Serial interface started");
setupTcpServer();
setupShutdownTimeoutWatchdog();
}
void loop() {
loopShutdownTimeoutWatchdog();
loopStatus();
loopTcpServer();
loopCommands();
setupMotors();
}
void setupMotors() {
stepper1.moveTo(200);
stepper2.moveTo(200);
stepper1.run();
stepper2.run();
}
void shutdownAllMotors() {
//for(int i=0; i<kNumberOfMotors; i++) {
//motor[i].dutyCycle = 0;
stepper1.setMaxSpeed(1.0);
stepper2.setMaxSpeed(1.0);
}
void fullSpeedAllMotors() {
stepper1.setMaxSpeed(200.0);
stepper1.setAcceleration(50.0);
stepper1.moveTo(500.0);
stepper2.setMaxSpeed(200.0);
stepper2.setAcceleration(50.0);
stepper2.moveTo(500.0);
}
/*** BEGIN TCP Server handling functions ***/
// Network settings
// BE SURE TO CHANGE THESE TO MATCH YOUR INTERFACE AND IP ADDRESS
// AND PORT OF YOUR ETHERNET MODULE!
//
// Also, the SD Card reader is EXPLICITLY DISABLED by setting pin
// 4 to high
void setupTcpServer() {
//pinMode(4, OUTPUT);
//digitalWrite(4, HIGH);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("ProtocolRunner: Server operational at ");
Serial.print(Ethernet.localIP());
Serial.print(" port ");
Serial.println(serverPort);
tcpShouldDisconnect = false;
}
// Services the TCP/IP interface and copies bytes received into
// tcpReceivedBuffer. This buffer is then read during the command parsing
// to extract commands to operate the motors.
void loopTcpServer() {
unsigned tcpReadBufferCursor = 0;
EthernetClient client = server.available();
for (int i=0; i<BUFFER_SIZE; i++) {
tcpReadBuffer[i] = '\0';
}
if (client) {
while (client.connected()) {
if (tcpShouldDisconnect) {
client.stop();
tcpShouldDisconnect = false;
}
if(client.available()) {
char symbol = client.read();
if (symbol == '\r') {
//Serial.print("TCP: ");
//Serial.println("Ignoring \\r");
} else if (symbol == '\n') {
//Serial.print("TCP command: ");
//Serial.println(tcpReadBuffer);
//client.println("whatever");
client.println(statusBuffer);
break;
} else {
if (tcpReadBufferCursor < BUFFER_SIZE) {
tcpReadBuffer[tcpReadBufferCursor] = symbol;
tcpReadBufferCursor++;
} else {
Serial.println("TCP: Buffer overflow caught!");
}
}
}
}
}
}
/*** END TCP Server handling functions ***/
/*** BEGIN Status looping function ***/
// Creates the strings for status reports to report back to client.
void loopStatus() {
snprintf(statusBuffer, BUFFER_SIZE, "READY,motorCount=%d,kShutdownTimeoutMillis=%u", kNumberOfMotors, kShutdownTimeoutMillis);
}
/*** END Status looping function ***/
/*** BEGIN command handling functions ***/
/*void setupCommands() {
}*/
// Pasrses and executes commands received over TCP/IP
void loopCommands() {
if (strlen(tcpReadBuffer) > 0) {
Serial.print("Command ");
Serial.print(strlen(tcpReadBuffer));
Serial.print(": ");
Serial.println(tcpReadBuffer);
char *cmd;
cmd = strtok(tcpReadBuffer, ",");
// Update the timestamp of the command.
//
// TODO: Actually, should probably be
// refactored into the individual successful command executions
// for maximum security
resetShutdownTimeoutWatchdog();
// This is somewhat buggy, because a disconnect doesn't seem to happen
// without an extra carriage return.
if (strcmp(cmd, "QUIT") == 0) {
Serial.println("Disconnect requested");
tcpShouldDisconnect = true;
} else if (strcmp(cmd, "status") == 0) {
Serial.println("Dummy status command");
} else if (strcmp(cmd, "motor") == 0) {
char *szMotorNumber = strtok(NULL, ",");
char *szMotorSpeed = strtok(NULL, ",");
int motorNumber = atoi(szMotorNumber);
int motorSpeed = atoi(szMotorSpeed);
if (!(motorNumber >= 0 && motorNumber < kNumberOfMotors)) {
motorNumber = 0;
Serial.println("Bad motor number requested");
}
// motor[motorNumber].dutyCycle = convertSpeedToPwmDutyCycle(motorSpeed);
/*Serial.print("Motor ");
Serial.print(motorNumber);
Serial.print(" duty_cycle ");
Serial.print(duty_cycle);
Serial.println(" requested");*/
} else if (strcmp(cmd, "full speed") == 0) {
fullSpeedAllMotors();
Serial.println("Setting all motors to full speed.");
} else if (strcmp(cmd, "all stop") == 0) {
shutdownAllMotors();
Serial.println("Stopping all motors");
} else {
Serial.println("User gave an unrecognized command.");
}
}
}
/*** END command handling functions ***/
/*** BEGIN watchdog and shutdown function ***/
// kShutdownTimeoutMillis and timestampOfLastCommand establish a timeout
// for motor operations in case connections break and we don't want the
// motors to run forever (or at least until the batteries run out)
void loopShutdownTimeoutWatchdog() {
if (millis() - timestampOfLastCommand > kShutdownTimeoutMillis) {
Serial.println("Shutting down because of watchdog");
shutdownAllMotors();
resetShutdownTimeoutWatchdog();
}
}
void setupShutdownTimeoutWatchdog() {
resetShutdownTimeoutWatchdog();
}
void resetShutdownTimeoutWatchdog() {
timestampOfLastCommand = millis();
Serial.println("Shutdown timeout watchdog reset");
}
/*** BEGIN watchdog and shutdown function ***/