Go Down

Topic: Arduino IO package and AccelStepper (Read 6214 times) previous topic - next topic

francis24

Hi, I am pretty new to all of this. The plan is to control two stepper motors with Accel Stepper via Matlab. So I was wondering-

1st) Is it possible to use an Arduino library like Accel Stepper via Matlab (using the Arduino IO package) - I have researched a bit but have not found anything on the internet so far dealing with this particular combination. In case this is possible is there anything special I have to take care of when installing Arduino IO? For example, I am not sure if I should upload  adio.pde  adioe.pde or adioes.pde for my needs.

2nd) I got some idea how the code would have to look like in the Arduino program, but now that I have to use Matlab- would  I have to change it into Matlab "speak"?

Many thanks in advance for any advice!

Isa

PaulS

Quote
1st) Is it possible to use an Arduino library like Accel Stepper via Matlab (using the Arduino IO package)

That is a Matlab question.

Quote
For example, I am not sure if I should upload  adio.pde  adioe.pde or adioes.pde for my needs.

Which of those sketches includes the AccelStepper library?

Quote
2nd) I got some idea how the code would have to look like in the Arduino program

One of those three you listed above?

Quote
but now that I have to use Matlab- would  I have to change it into Matlab "speak"?

Why do you HAVE to use Matlab, and what is "matlab-speak"?

francis24

#2
Oct 13, 2014, 12:56 pm Last Edit: Oct 13, 2014, 03:24 pm by francis24 Reason: 1

I have tried contacting Matlab but have unfortunately had no success in getting a reply in their forum, so I thought I give it a try here. I just need to find somebody who has worked with Accel Stepper and Matlab before and I reckoned that would be easier in the Arduino Forum.

Quote
For example, I am not sure if I should upload  adio.pde  adioe.pde or adioes.pde for my needs.
Which of those sketches includes the AccelStepper library?


It does not say anything about Accel Stepper in the readme file, just the following :

"The following sketches are provided with the package:

-) adio.pde     : analog and digital IO, plus basic serial commands only
-) adioe.pde    : adio.pde + encoders support
-) adioes.pde   : adioe.pde + servo support
-) motor_v1.pde : adioes.pde + afmotor v1 shield
-) motor_v2.pde : adioes.pde + afmotor v2 shield

In most cases the adioes.pde will cover all you need. The last two sketches
are needed for the Adafruit Motor Shield (version 1 and 2 respectively)."

I am not using the motor shield though, but two Big Easy Driver.


Quote
2nd) I got some idea how the code would have to look like in the Arduino program
One of those three you listed above?


I meant I have a few lines of code written together in the Arduino that make my Motors turn the way I want with the help of Accel Stepper, just something along the lines of
"#include <AccelStepper.h>

// Define two steppers and the pins they will use
AccelStepper stepper1(1, 9, 8 );
AccelStepper stepper2(1, 7, 6 );



void setup()
{  
 stepper1.setMaxSpeed(1000);
 stepper1.setSpeed(1000);
 stepper2.setMaxSpeed(1000);
 stepper2.setSpeed(1000);
}

void loop()
{

 stepper1.runSpeed();
 stepper2.runSpeed();
} "

Quote
but now that I have to use Matlab- would  I have to change it into Matlab "speak"?
Why do you HAVE to use Matlab, and what is "matlab-speak"?


I am using the Arduino within an experimental setup and other parts of that setup require to run the whole thing in Matlab, unfortunately. With Matlab speak and that question I was just wondering if when I use Matlab do I have to change the above code to align with Matlab programming language?

Thanks for your help!



Robin2

I don't know Matlab but the Matlab program will be quite separate from the Arduino program except that the oe must be able to talk to the other.

I presue those Arduino programs that you listed are designed save you the trouble of writing your own Arduino code to communicate with Matlab. But if you know the format of the data that Matlab sends (or expects to receive) you could write an Arduino program to get data from it.

Maybe if you post the code from one of the Arduino programs you have mentioned we could give you a more useful answer. It may be very easy to integrate with AccelStepper.

And do you have to use Matlab?

What sort of data do you want Matlab to send to the Arduino to control the stepper motor?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

francis24

Hi, thanks for your help!I think what I want to do is actually quite simple but me not being able to wrap my head around interfacing is the problem. In simple words, I got my Arduino, two Big Easy Driver and two steppers and I need to be able to make Matlab prompt the Arduino to execute certain lines of code, then wait, then again execute certain lines of code. So it would look like something like this: Matlab does something (in my case sending a parallel port signal to an EEG recording machine), then the Arduino needs to make the steppers move to a certain position and stop, then Matlab does something (displaying a message on the computer screen and sending again another EEG trigger), and then the arduino needs to move the steppers again etc. My Arduino code isnt ready yet but it would look similar to this

Code: [Select]

#include <AccelStepper.h>

AccelStepper stepper1(1, 9, 8);

void setup()

    stepper.setMaxSpeed(200.0);
    stepper.setAcceleration(100.0);
}

void loop()
{   
    stepper.runToNewPosition(0);
    stepper.runToNewPosition(500);
// now I want the Arduino to stop and wait til Matlab tells it again to execute the next line
    stepper.runToNewPosition(100);
// again now I want the Arduino to stop and wait til Matlab tells it again to execute the next line
    stepper.runToNewPosition(120);
}


So from what I have gathered on the forum / internet so far is that I need to let Matlab communicate with the Arduino via a standard serial object. I have been able to connect the Arduino to Matlab, as in I can control the LED on the Arduino via Matlab now, but how do I get the Arduino to execute bits of code and then wait within the loop until Matlab tells it to continue?
Many thanks in advance!

PaulS

Quote
// now I want the Arduino to stop and wait til Matlab tells it again to execute the next line


That's not the right approach. You want Matlab to send something like "<M500>" to the Arduino. When serial data arrives, you want the Arduino to read and store it, starting when the start-of-packet marker ('<') arrives, ending when the end-of-packet marker ('>') arrives. When the end of packet marker arrives, the Arduino should then parse the stored data to determine that it needs to call stepper.moveTo() (the 'M') with 500 as the number of steps (the "500" part of the data).

Robin2

What @PaulS says is pretty much essential if you want Matlab to send instructions about how many steps to take. The Arduino code in this demo works in the way that Paul has mentioned.

However I got the impression from your earlier posts that the Arduino already knows how many steps to take and it just needs a "start" signal from Matlab.

If that is true, and if you can already send code from Matlab to light either of 2 LEDs then it would easy to modify the Arduino code so that, instead of lighting an LED (or as well as) it calls a function that causes the appropriate stepper moves.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

francis24

Yes, exactly, Robin-I do not need to send the number of steps from matlab to arduino, I already have them in the Arduino code. But giving that "start" signal ( and respective stop signal) from Matlab to Arduinio is my problem.
For switching the LED on and off I uploaded the Arduino IO for Matlab (http://www.mathworks.co.uk/matlabcentral/fileexchange/32374-matlab-support-for-arduino--aka-arduinoio-package-) onto the Arduino and then entered this in matlab:

Code: [Select]
a=arduino('COM4')
a.pinMode(13,'OUTPUT');
digitalWrite(a,13,1);
pause(2);
digitalWrite(a,13,0);

delete(a)


So I can activate a pin on the Arduino now via Matlab but how do I prompt arduino to execute parts of this code?
:
Code: [Select]
AccelStepper stepper1(1, 9, 8);

void setup()
{  
   stepper.setMaxSpeed(200.0);
   stepper.setAcceleration(100.0);
}

void loop()
{    
   stepper.runToNewPosition(0);
   stepper.runToNewPosition(500);
// now I want the Arduino to stop and wait til Matlab tells it again to execute the next line
   stepper.runToNewPosition(100);
// again now I want the Arduino to stop and wait til Matlab tells it again to execute the next line
   stepper.runToNewPosition(120);
}


Isa

Robin2


Yes, exactly, Robin-I do not need to send the number of steps from matlab to arduino, I already have them in the Arduino code. But giving that "start" signal ( and respective stop signal) from Matlab to Arduinio is my problem.
For switching the LED on and off I uploaded the Arduino IO for Matlab (http://www.mathworks.co.uk/matlabcentral/fileexchange/32374-matlab-support-for-arduino--aka-arduinoio-package-) onto the Arduino and then entered this in matlab:

Code: [Select]
a=arduino('COM4')
a.pinMode(13,'OUTPUT');
digitalWrite(a,13,1);
pause(2);
digitalWrite(a,13,0);

delete(a)



Sorry, I don't know enough about Matlab to make any sense of that. It looks like a variation of Arduino code but you say is is on Matlab. I had assumed Matlab has simple instructions whose effect is (for example) "send the character A to the serial port where the Arduino is connected".

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

francis24

#9
Oct 17, 2014, 03:50 pm Last Edit: Oct 17, 2014, 05:53 pm by francis24 Reason: 1
I played around a few hours and actually figured out how I can send variables from Matlab to Arduino, just as you said. I just have to use:

Code: [Select]
>>arduino=serial('COM4','BaudRate',9600)
>> fopen(arduino)
>> fprintf(arduino,'1')
>> fprintf(arduino,'2')
>> fclose(arduino)


in Matlab. To get the LED to light up, I would use


Code: [Select]

void setup()
{  


pinMode(13, OUTPUT);
digitalWrite(13,LOW);
 Serial.begin(9600);
}
void loop()
{    
 if(Serial.available() > 0)
 {
   char letter =  Serial.read();
   if (letter=='1')
   {
      digitalWrite(13,HIGH);
   }
else if (letter=='2')
 { digitalWrite(13,LOW);
   }
}
}



on the Arduino. But how do I combine this code with the stepper code from the previous post though in order to move the stepper when I send a variable from Matlab? I tried:
Code: [Select]
#include <AccelStepper.h>


AccelStepper stepper1(1, 9, 8);


void setup()
{  

  stepper1.setMaxSpeed(3200);
pinMode(13, OUTPUT);
digitalWrite(13,LOW);
 Serial.begin(9600);
}
void loop()
{    
 if(Serial.available() > 0)
 {
   char letter =  Serial.read();
   if (letter=='1')
   {
      digitalWrite(13,HIGH);
      stepper1.moveTo(6400);
 stepper1.setSpeed(3200);
 stepper1.runSpeedToPosition();
   }
else if (letter=='2')
 { digitalWrite(13,LOW);
 stepper1.moveTo(6400);
 stepper1.setSpeed(500);
 stepper1.runSpeedToPosition();
   }
}
}


but nothing would turn. On the Arduino serial communication website it states that
Quote
All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.  


Could that be it?after all my BED driven stepper is using pins (1,8,9). However, I tried AccelStepper stepper1(2, 9, 8 ) and while this is working as long as I activate the code directly in the Arduino sketch, once again nothing moves if I try it via serial communication via Matlab .



Isa

Robin2

The 1 in the "(1,8,9)" is not a pin number. It is a signal to the AccelStepper library that you are using a stepper driver board that takes step and direction signals. I presume you have step and direction connected to the correct pins?

Have you managed to get the stepper motor to move in a short sketch on its own? If not, that should be your first task. Then you can import the working code into your other sketch.

I'm not an expert on AccelStepper but your code looks OK. What does runSpeedToPosition() do?

I presume the LED switches on and off properly?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

francis24

Ok, that is good to know about the 1 in there, thanks!
All the wires are connected properly, the sketch without any of the serial communication bits works perfectly. In the words of the people who came up wiht AccelStepper,  runSpeedToPosition() "Moves the motor to the new target position and blocks until it is at position. Dont use this in event loops, since it blocks". I have also tried a non-Accel Stepper dependent sketch with and without the serial communication code bits:

Code: [Select]
void setup() {               
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
}

void loop() {
  digitalWrite(9, HIGH);
  delayMicroseconds(100);         
  digitalWrite(9, LOW);
  delayMicroseconds(100);
{


but once again, without the serial communication bits it works, but with the serial communication bits in it, when I try to give the "start signal" with Matlab nothing happens.  The LED works properly too, I wrote the digitalwrite function for the LED into those motor sketches and can turn it on and off with no problems via giving the signal with Matlab, just the motor doesn't react at all.

Isa

Robin2

I just realized there was something else I had intended to say.

Code: [Select]
>>arduino=serial('COM4','BaudRate',9600)
>> fopen(arduino)
>> fprintf(arduino,'1')
>> fprintf(arduino,'2')
>> fclose(arduino)


When Matlab opens the serial port the Arduino will reset and that takes several seconds during which time it is pointless for Matlab to send any data. As I said earlier, I don't know Matlab. The best way to deal with this is for the Arduino to send a short message from setup() - for example Serial.println("Arduino is Ready"); - and for the PC program to wait for that before it sends any data. If you can't do that with Matlab, perhaps delaying for 10 seconds after fopen would be suitable.

And, consistent with this, don't use fclose until you have finished all communication with the Arduino.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

francis24

Hi,
I have found the mistake I made! Due to the nature of stepper1.runSpeedToPosition(); it has to be in the big loop, not the small one, in order for the motors to turn. Rookie mistake, I suppose.

With this code, I the start signal from Matlab prompts the Arduino to light up the LED and make the motors turn the way I want them too.

Code: [Select]

#include <AccelStepper.h>

// Define two steppers and the pins they will use
AccelStepper stepper1(1, 9, 8);



void setup()
{


      pinMode(13, OUTPUT);
      digitalWrite(13,LOW);
       Serial.begin(9600);
       stepper1.setMaxSpeed(3200);
       stepper1.setSpeed(3200);
}
void loop()
{
       if(Serial.available() > 0)
{
         char letter =  Serial.read();
         if (letter=='1')
         {
            digitalWrite(13,HIGH);
            stepper1.moveTo(10000);

         }
else if (letter=='2')
       {
            digitalWrite(13,LOW);
             stepper1.moveTo(1024);

        }
}
stepper1.runSpeedToPosition();
}



Thanks so much for your help and advice! I will make sure to take care of the resetting time too! I hope this thread will prove helpful for other people out there.


Go Up