0

I am driving two stepper motor using serial data from Raspberry Pi to Arduino. Motors work fine when i am passing serial data but once i stop sending serial data, it doesn't accept serial data again unless I burn the program again in arduino. Here is code for that:

 #include <Driver.h>
//////////////////////////////////////////////////////////////////////////////////////////
/*
 Hardware pins defined in this section as connected from arduino to Pan DRV8834
 controller and tilt DRV8834 controller.M0 and M1 pins are common for both controllers
 ,hence declared only once(This is done to save number of pins, connecting it to
 additional pins , provide no special advantage since both both motors should be set
 to same level of microstepping).SLP pin is not currently used.
*/
//////////////////////////////////////////////////////////////////////////////////////////
#define PAN_DIRECTION_PIN 8
#define TILT_DIRECTION_PIN 7
#define PAN_STEP_PIN 10
#define TILT_STEP_PIN 9
#define M0 6
#define M1 5
#define MOTOR_SPEED 200
#define SLP 0
///////////////////////////////////////////////////////////////////////////////////////////
#define BAUD_RATE 230400 //Baud Rate for serial communication
///////////////////////////////////////////////////////////////////////////////////////////
/*
 field data for receiving buffer on arduino side , important for serial communication
*/
#define NUMBER_OF_FIELDS 2 // how many comma separated fields we expect
int fieldIndex = 0; // the current field being received
int values[NUMBER_OF_FIELDS]; // array holding angles for both motors
const char startChar = '<'; //beginning character of a valid command
const char endchar = '>'; //this will be ending character to ensure a valid command has ended
boolean storeString = false; //This will be our flag to put the data in our buffer
#define zeroCharacter 'z'
bool zeroCommand = false;
#define DATABUFFERSIZE 50
char dataBuffer[DATABUFFERSIZE + 1]; //Add 1 for NULL terminator
int dataBufferIndex = 0;
int procBufCounter; //iterate through whole string to obtain the commands
float panAngle, tiltAngle;
int sign = 1;
 Driver stepper_pan(M0, M1, PAN_STEP_PIN, SLP, PAN_DIRECTION_PIN);
 Driver stepper_tilt(M0, M1, TILT_STEP_PIN, SLP, TILT_DIRECTION_PIN);
 void processCommand(char *buffer) {
 procBufCounter = 0;
 bool startprocessing = false;
 bool endprocessing = false;
 values[0] = 0;
 values[1] = 0;
 while ((procBufCounter < DATABUFFERSIZE) && (!endprocessing)) {
 char ch = buffer[procBufCounter]; //read the character while simulatneously increasing the buffer cursor
 if (ch == '<') {
 startprocessing = true;
 }
 else if (ch == '>') {
 values[fieldIndex] = values[fieldIndex] * sign ;
 fieldIndex = 0;
 endprocessing = true;
 }
 if (startprocessing)
 {
 if (ch == ',') {
 values[fieldIndex] = values[fieldIndex] * sign ; // set value to the accumulated value
 sign = 1;
 fieldIndex++;
 }
 else if ('0' <= ch && ch <= '9' ) {
 values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
 }
 else if (ch == '-') {
 sign = -1;
 }
 else if(ch == 'z'){
 zeroCommand = true;
 }
 }
 procBufCounter++;
 }
 panAngle = ((float)(values[0]))/100;
 tiltAngle = ((float)(values[1]))/100;
 bool panMoved = true, tiltMoved = true;
 stepper_pan.moveTo(panAngle);
 stepper_tilt.moveTo(tiltAngle);
 while (panMoved || tiltMoved) {
 panMoved = stepper_pan.run();
 tiltMoved = stepper_tilt.run();
 }
 if(zeroCommand){
 stepper_pan.reset_positions();
 stepper_tilt.reset_positions();
 zeroCommand = false;
 }
 }
 //////////////////////////////////////////////////////////////////////////////////////
 void setup() {
 stepper_pan.setStepMode(STEP_THIRTYTWO);
 stepper_pan.setSpeed(MOTOR_SPEED);
 stepper_tilt.setStepMode(STEP_THIRTYTWO);
 stepper_tilt.setSpeed(MOTOR_SPEED);
 Serial.begin (BAUD_RATE);
 Serial.flush();
 }
 void loop() {
 if (processString) {
 processCommand(dataBuffer);
 processString = false;
 // dataBuffer = "";
 }
 }
 void serialEvent() {
 static int dataBufferIndex = 0;
 while (Serial.available() > 0) {
 char incomingbyte = Serial.read();
 if (incomingbyte == startChar) {
 dataBufferIndex = 0; //Initialize our dataBufferIndex variable
 storeString = true;
 }
 if (storeString) {
 //Let's check our index here, and abort if we're outside our buffer size
 //We use our define here so our buffer size can be easily modified
 if (dataBufferIndex == DATABUFFERSIZE) {
 //Oops, our index is pointing to an array element outside our buffer.
 dataBufferIndex = 0;
 break;
 }
 if (incomingbyte == endchar) {
 dataBuffer[dataBufferIndex] = endchar; //null terminate the C string
 dataBuffer[dataBufferIndex + 1] = '0円';
 //Our data string is complete. return true
 storeString = false;
 processString = true;
 dataBufferIndex = 0;
 }
 else {
 dataBuffer[dataBufferIndex++] = incomingbyte;
 dataBuffer[dataBufferIndex] = 0; //null terminate the C string
 }
 }
 }
 }
asked Jul 28, 2016 at 3:37
2
  • It's difficult to debug your code because it's not complete, I think there is quite a bit of code in parameters.h. Also the commented out LCD code is a distraction. Is it possible to edit your post to have al of the relevant code? (Then people can copy it and debug it) Commented Jul 28, 2016 at 6:37
  • @Matt I have edited the code , can you please have a look now, thanks! Commented Jul 28, 2016 at 6:44

1 Answer 1

1

I finally figured it out. It is due to reset whenever I open a port for serial communication. Attaching a 10uF capacitor between ground and reset fixed everything.

answered Jul 30, 2016 at 5:06
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.