Jump to content
 

DCC Controlled (PECO) Turntable Project using a Arduino Uno


Recommended Posts

A very simple way to do this would be to show the positions on the Serial monitor as you adjust the settings. Make a note of them and put them into the program code.

 

...R

That's true Robin, but having it 'programmable' without modifying the code would be the icing on the cake.

 

Ray.

Link to post
Share on other sites

That's true Robin, but having it 'programmable' without modifying the code would be the icing on the cake.

 

Ray.

Robin & Ray-

 

The way it works now, it just shows in the monitor what the value needs to be increased by.  The problem is that the "adjustments" also affect the "home" position.  So you need to reset the Arduino once you read the values from the Serial monitor.  It would be super nice if you could output a real location to the serial monitor and not have to restart each time you move using the programming buttons.  I have made it work, but for future tables, something easier to interface would be ideal. 

 

Talk soon

-Eric

Link to post
Share on other sites

Part 5 - Testing the Reference Sensor and setting the Speed and Acceleration

 

Firstly mark a point on the end of the turntable deck containing the magnet so you can check the alignment and repeatability of the test.

 

For this test we need to create a new sketch for the code.

 

Start the Arduino IDE and from the 'File' drop down menu select 'New'

 

Now copy/paste the following code into the new sketch.

 

// Turntable test routine

 

 

Ray:

 

Inspired by your thread (and although not a programmer) I am attempting to understand the code used in the DCC Stepper Motor Controlled (PECO) turntable project.   I haven’t yet built the DCC interface board so I have been trying to get to grips with the basic stepper motor control.

 

 I have adapted one of the Adafruit stepper motor sketches so that it allows me to run the stepper motor continuously at 200 steps per revolution.  I then tried to adapt the sketch in your post #17 (19 Nov 2013) to see if I could get it to continuously rotate at 3200 steps per revolution.   In doing this I have deleted your references to the sensor pins.  The resulting sketch (below) compiles OK but on uploading to the Arduino the stepper motor clicks away backward and forward a couple of degrees or so for a few seconds and then stops.  I would be most grateful for any help you could provide on what I am doing wrong. 

 

DEVELOPMENT_TURNTABLE.ino

 

Simond:

 

Although I haven't yet  build the DCC interface board I have purchased a couple of Maplin reflector sensors as mentioned in  your post 57 on this topic.   They are indeed very small (3mm square) and I wondered if you have used one for this application.  If so a picture of the wired device would be extremely useful!

 

Thanks and Best wishes

 

CC

Link to post
Share on other sites

Ray:

 

Inspired by your thread (and although not a programmer) I am attempting to understand the code used in the DCC Stepper Motor Controlled (PECO) turntable project.   I haven’t yet built the DCC interface board so I have been trying to get to grips with the basic stepper motor control.

 

 I have adapted one of the Adafruit stepper motor sketches so that it allows me to run the stepper motor continuously at 200 steps per revolution.  I then tried to adapt the sketch in your post #17 (19 Nov 2013) to see if I could get it to continuously rotate at 3200 steps per revolution.   In doing this I have deleted your references to the sensor pins.  The resulting sketch (below) compiles OK but on uploading to the Arduino the stepper motor clicks away backward and forward a couple of degrees or so for a few seconds and then stops.  I would be most grateful for any help you could provide on what I am doing wrong. 

 

attachicon.gifDEVELOPMENT_TURNTABLE.ino

 

Simond:

 

Although I haven't yet  build the DCC interface board I have purchased a couple of Maplin reflector sensors as mentioned in  your post 57 on this topic.   They are indeed very small (3mm square) and I wondered if you have used one for this application.  If so a picture of the wired device would be extremely useful!

 

Thanks and Best wishes

 

CC

 

 

I noticed that the attached .ino file was not visible as text  in my previous post so I am attaching a text version:

 

/*This interim test routine attempts to enable the continuous rotation of a stepper motor

at 3200 steps/revolution and is adapted by CC from the Turntabable routine posted by Tender on

RM Web on 19 Nov 2013  under the topic DCC Controlled (PECO) turntable project post #17.

Requires the Adafruit_Motorshield v2 library

https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library

And AccelStepper with AFMotor support

https://github.com/adafruit/AccelStepper

This sketch is for Adafruit Motorshield v2 only!

Will not work with v1 shields */

 

#include <AccelStepper.h>

#include <Wire.h>

#include <Adafruit_MotorShield.h>

#include "utility/Adafruit_PWMServoDriver.h"

 

Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

// Connect a stepper motor with 200 steps per revolution (1.8 degree)

// to motor port #2 (M3 and M4)

 

Adafruit_StepperMotor *mystepper2 = AFMStop.getStepper(200, 2);

 

// you can change these to SINGLE, DOUBLE, INTERLEAVE or MICROSTEP!

// wrapper for the motor! (3200 Microsteps/revolution)

void forwardstep2() { 

  mystepper2->onestep(FORWARD, MICROSTEP);

}

void backwardstep2() { 

  mystepper2->onestep(BACKWARD, MICROSTEP);

}

// Now we'll wrap the stepper in an AccelStepper object

AccelStepper stepper2(forwardstep2, backwardstep2);

void setup()

    AFMStop.begin(); // Start the shield

 

// set stepper speed, acceleration and position

  stepper2.setMaxSpeed(100.0);

  stepper2.setAcceleration(10.0);

  stepper2.moveTo(3200); 

 

}

void loop()

{

 stepper2.run();

    

}

 

PS how do you include .ino files as visible text in your posts?

 

CC

Link to post
Share on other sites

Hi CC.

 

When the sketch starts it set the current position to 0. Your sketch then moves the current position to 3200 [stepper2.moveTo(3200)] which is only one step away so it has virtually nowhere to go. Try stepper2.moveTo(1600) which should turn 180 degrees.

 

To put code in to a post copy the code from the IDE then click on the arrows (<>) in the RMweb post toolbar, and then paste the code into the pop up box.

 

Ray.

 

PS might be worth posting the sketch you used to rotate continuously at 200 steps per rev.

Edited by tender
Link to post
Share on other sites

I have been delving into the "black box" that is the DCC code detection process and I have written what I hope is a simple Arduino program to illustrate the process. I have put it in the Arduino Thread so as not to confuse things here. http://www.rmweb.co.uk/community/index.php?/topic/82978-arduino-applications-and-programs/page-2&do=findComment&comment=1400617 Its at Reply #33.

 

...R

Link to post
Share on other sites

Ray:

 

Inspired by your thread (and although not a programmer) I am attempting to understand the code used in the DCC Stepper Motor Controlled (PECO) turntable project.   I haven’t yet built the DCC interface board so I have been trying to get to grips with the basic stepper motor control.

 

 I have adapted one of the Adafruit stepper motor sketches so that it allows me to run the stepper motor continuously at 200 steps per revolution.  I then tried to adapt the sketch in your post #17 (19 Nov 2013) to see if I could get it to continuously rotate at 3200 steps per revolution.   In doing this I have deleted your references to the sensor pins.  The resulting sketch (below) compiles OK but on uploading to the Arduino the stepper motor clicks away backward and forward a couple of degrees or so for a few seconds and then stops.  I would be most grateful for any help you could provide on what I am doing wrong. 

 

attachicon.gifDEVELOPMENT_TURNTABLE.ino

 

Simond:

 

Although I haven't yet  build the DCC interface board I have purchased a couple of Maplin reflector sensors as mentioned in  your post 57 on this topic.   They are indeed very small (3mm square) and I wondered if you have used one for this application.  If so a picture of the wired device would be extremely useful!

 

Thanks and Best wishes

 

CC

 

CC

 

Couple of things - I found that I got exactly the same issue of the stepper grunting and wandering about a bit when it boots. Still don't know why.

 

This was the impetus required to get me to go to Maplin and buy the photosensor!

 

Pictures as follows

post-20369-0-18964300-1396200049.jpg

Sensor buried in the ballast in the turntable well - I drilled 4 small holes and dropped the legs through. Ballast needs refinishing in parts, and I'll touch around the sensor with a bit of brown to tone it in a bit.

 

post-20369-0-83468000-1396200085.jpg

I soldered the two bias resistors and the leads directly to the legs of the sensor. Red is +5V, black is ground, green is output. Ignore the orange, which is the feed to the turntable centre pivot from the polarity relay. As you can see, I araldited the wires to the board to stop the pins of the sensor breaking, or shorting.

 

post-20369-0-70527700-1396200109.jpg

Glued white plastic sheet under the t/t and added a layer to reduce the clearance to a minimum - apparently, they differentiate better with a really small distance to the reflective item

 

I played around with the threshold value of the sensor detection, and found that it was typically reading around 600 when open "to the sky", a bit higher as the t/t covered it, and then dropped to less than 50 when the white plastic card covered it. I set the program to detect something like 200

 

post-20369-0-47850600-1396200133.jpg and this is the new "brain".

Arduino pro mini, and Pololu stepper driver on a veroboard base. I've been wiring it up this week, and was hoping to have this all working first time but fortunately / unfortunately I discovered I had got the Arduino pinouts mirrored when I drew my schematic, so I'm back to the drawing board on this one - fortunately no damage done except to my ego!

 

Best

Simon

Link to post
Share on other sites

Hi CC.

 

When the sketch starts it set the current position to 0. Your sketch then moves the current position to 3200 [stepper2.moveTo(3200)] which is only one step away so it has virtually nowhere to go. Try stepper2.moveTo(1600) which should turn 180 degrees.

 

To put code in to a post copy the code from the IDE then click on the arrows (<>) in the RMweb post toolbar, and then paste the code into the pop up box.

 

Ray.

 

PS might be worth posting the sketch you used to rotate continuously at 200 steps per rev.

 

 

Ray -

 

Many thanks for your very prompt response.   I have tried out your suggestion (stepper2.moveTo(1600)) but unfortunately nothing changes and the stepper motor still oscillates backward and forward about two degrees for several seconds and then slowly stops.    I'm still scratching my head on this one and trying various changes to the code but to no avail up until now.  I will let you know if I succeed.  Meanwhile if you have any other suggestions I would be most grateful for them.

 

Below I include the sketch for continuous rotation of the stepper at 200 dpi which is a modification of one of the Adafruit stepper motor examples.   I have included some print statements in the code so that I can see what is happening on the serial monitor

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control.  

The sketch is adapted to provide continuous rotation of the stepper at 200
steps per revolution.

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438

This sketch enables continuous rotation of a stepper motor
Connect a unipolar/bipolar stepper to M3/M4

*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"


// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myStepper = AFMS.getStepper(200, 2);


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper motor continuous!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // setup the stepper
  myStepper->setSpeed(10);  // 10 rpm   
}

int i;
void loop() {
 
  for (i=0; i<255; i++) {
     
    myStepper->step(1, FORWARD, DOUBLE);
   
   delay(0);
 
 
  Serial.println("Stepper motor going!");
 
 for (i=255; i!=0; i--) 
    
    myStepper->step(1, FORWARD, DOUBLE);
    delay(0);
    
 Serial.println("Stepper motor continuing!");    
    
 
}
}

Simond -

 

Thanks very much for your response and the included pictures (which I will study carefully!).

 

 

Regards to you both

 

 

CC

.

Link to post
Share on other sites

Ray -

 

Many thanks for your very prompt response.   I have tried out your suggestion (stepper2.moveTo(1600)) but unfortunately nothing changes and the stepper motor still oscillates backward and forward about two degrees for several seconds and then slowly stops.    I'm still scratching my head on this one and trying various changes to the code but to no avail up until now.  I will let you know if I succeed.  Meanwhile if you have any other suggestions I would be most grateful for them.

 

Below I include the sketch for continuous rotation of the stepper at 200 dpi which is a modification of one of the Adafruit stepper motor examples.   I have included some print statements in the code so that I can see what is happening on the serial monitor

 

Simond -

 

Thanks very much for your response and the included pictures (which I will study carefully!).

 

 

Regards to you both

 

 

CC

.

 

Ray/Simon

 

Problem sorted and in the end it wasn't a problem with the code at all!   When I assembled the motor shield I was guided the instructions on the Adafruit website.  The instructions also show a stepper motor connected to the M3/M4 terminals of the shield and so I therefore placed the four coloured wires from the motor into the same slots shown on the Adafruit website.   Having exhausted all my ideas for modifying the code I went back through the first page of this topic and realised they were not in the same slots that you had used.   My error.   Changing to the slots shown in your picture in post #8 has had a dramatic effect and I can now run the stepper smoothly using the MICROSTEP function.   So beware any of you that use the Adafruit photo of the connected stepper motor as a reference.   It is for left hand drive models only!

 

I wouldn't say that the time I have spent has all been wasted - I have learnt a lot about the code in the Adafruit stepper motor Arduino code examples.   In fact I have constructed about the simplest Sketch possible to run the stepper a full revolution backwards and forwards smoothly, slowly and very quietly.   It is based on the Adafruit stepper example and you will see that it does not does not include the Accelstepper library.    This means that the number of steps you can set in one revolution is limited to 200 even though it is microstepping between steps.  

 

Now I  progress to explore your code further and investigate how the Accelstepper library of functions can give more precise control.  Presumably it enables each step to be divided up into 16 segments giving an overall 3200 step resolution per revolution.  Once I have mastered this I will be building the DCC interface using Simond's  last post on the topic as a very helpful guide.

 

The simple sketch mentioned earlier appears below:

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control.

The original ADAfruit sample sketch has been adapted by CC on 31-Mar-2014
to rotate a stepper motor forward for one revolution (200 steps) using
the MICROSTEP function, pauses for one second and then reverses for one revolution.
It will continue this cycle until powered down.  Speed has been set to 2
revolutions per minute but this can easily be changed by altering the variable
in the setSpeed command.  As set up a complete cycle takes around one and a half
minutes to complete.

Serial print commands have been incorporated in the sketch to enable operation 
to be monitored with the serial monitor facility of the Arduino IDE.

The sketch is the precursor to using the stepper to control the movements of a
turntable under DCC as described by Tender's thread on RMWeb "DCC Controlled (PECO) 
Turntable Project started on 12-Nov-2013. 

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438
*/


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test with microsteps for turntable");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  myMotor->setSpeed(2);  // 2 rpm   
}

void loop() {
  
  Serial.println("starting loop with microsteps");
   
  myMotor->step(200, FORWARD, MICROSTEP);  
  
  Serial.println("gone through loop forwards");
  
  delay(1000);
  
  myMotor->step(200, BACKWARD, MICROSTEP);
  
  Serial.println("gone through loop backwards");
  
}

Best wishes

 

CC

Link to post
Share on other sites

Hi CC,

Glad you got that sorted out. The AccelStepper also gives you the ability to change the rate of acceleration/deceleration from and to standstill. Important when you have a loco sitting on the turntable.

 

Ray.

Link to post
Share on other sites

Hi CC,

Glad you got that sorted out. The AccelStepper also gives you the ability to change the rate of acceleration/deceleration from and to standstill. Important when you have a loco sitting on the turntable.

 

Ray.

 

Hello Ray

 

At  long last I'm beginning to get to grips with the AccerStepper functions though I'm still struggling a bit.  As I am making just a little bit of progress I thought that I would start to look at the DCC aspects of your program in the complete code (Arduino Sketch) for turntable control in your post #48.  When I entered this into the Arduino IDE as a sketch and then tried to verify it I get an error message for the line:

 

DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);

 

saying 'DCC' was not declared in this scope.

 

I haven't a clue why this is occurring.  Can you help?

 

Best wishes

CC

Link to post
Share on other sites

Hi CC

 

Yep, know exactly what that is, easily missed, there is a set of libraries missing from the original post for part 5 which if forgot about. I should get around to editing the post.

 

PhilNE  picked up on this in his post

 

http://www.rmweb.co.uk/community/index.php?/topic/78578-dcc-controlled-peco-turntable-project/?p=1242019

 

Make sure you've included the DCC libraries as well.

 

See http://www.rmweb.co.uk/community/index.php?/topic/78578-dcc-controlled-peco-turntable-project/?p=1231245

 

Once you've downloaded these and the DCC libraries should work ok. 

Edited by tender
Link to post
Share on other sites

Hi CC

 

Yep, know exactly what that is, easily missed, there is a set of libraries missing from the original post for part 5 which if forgot about. I should get around to editing the post.

 

PhilNE  picked up on this in his post

 

http://www.rmweb.co.uk/community/index.php?/topic/78578-dcc-controlled-peco-turntable-project/?p=1242019

 

Make sure you've included the DCC libraries as well.

 

See http://www.rmweb.co.uk/community/index.php?/topic/78578-dcc-controlled-peco-turntable-project/?p=1231245

 

Once you've downloaded these and the DCC libraries should work ok. 

 

Ray

 

Thanks for the very prompt response.

 

I have looked in my Arduino Folder and see that I have downloaded the appropriate libraries as you see in the screenshot pictures below:

 

post-12469-0-54316600-1396815985.jpg

 

Arduino Folder

 

post-12469-0-08828600-1396816047.jpg

 

I have checked your code and all the requisite declarations are there but I still get the same message as you see -

 

post-12469-0-07755800-1396816253.jpg

 

I cannot understand what is happening

 

Regards

 

CC

Link to post
Share on other sites

Hi CambrianCoaster,

 

You could try these:

 

  • Move your DCC_Decoder library folder up one level, so that it sits in the libraries folder.
  • At the top of your sketch, before any other lines of code, make sure you have this line '#include <DCC_Decoder.h>', it tells the sketch compiler to pull in the DCC_Decoder library including all it's methods and functions. The line that is causing the problem in your code, is making a call to the library. It looks like it can't find it.

Also, is the 'dcc_decoder_v4' folder your sketch folder?, ideally you shouldn't keep this in the libraries folder. It would be better to move this into your Documents folder under the Arduino folder that was probably created. This is where you should work on your code. Don't mean to tell you how to suck eggs though.

 

Hope that helps.

 

Cheers, Mark.

Edited by Vonzack
Link to post
Share on other sites

Hi CC

 

Mark seems to have hit on your problem before I got to look into it. Looks like the DCC_Decoder library is one level too deep.

 

your C:\Program Files\Arduino\Libraries\ Folder should have these library folders.

 

AccelStepper

Adafruit_MotorShield

DCC_Decoder

 

 

Ray.

Link to post
Share on other sites

Hi CambrianCoaster,

 

You could try these:

 

  • Move your DCC_Decoder library folder up one level, so that it sits in the libraries folder.
  • At the top of your sketch, before any other lines of code, make sure you have this line '#include <DCC_Decoder.h>', it tells the sketch compiler to pull in the DC_Decoder library including all it's methods and functions. The line that is causing the problem in your code, is making a call to the library. It looks like it can't find it.

Also, is the 'dcc_decoder_v4' folder your sketch folder?, ideally you shouldn't keep this in the libraries folder. It would be better to move this into your Documents folder under the Arduino folder that was probably created. This is where you should work on your code. Don't mean to tell you how to suck eggs though.

 

Hope that helps.

 

Cheers, Mark.

 

 

Hi CC

 

Mark seems to have hit on your problem before I got to look into it. Looks like the DCC_Decoder library is one level too deep.

 

your C:\Program Files\Arduino\Libraries\ Folder should have these library folders.

 

AccelStepper

Adafruit_MotorShield

DCC_Decoder

 

 

Ray.

 

Mark/Ray:

 

I have moved the DCC_Decoder and _MACOSX folders up a level to the libraries folder as you suggest and the sketch compiles as it should.   I have also tried compiling the two examples (DCC_BasicAcc_Decoder  and DCC_Monitor) provided in the DCC Decoder library and they also compile OK.  

 

So many thanks to both of you - I would never have figured it out myself.

 

Best wishes

 

CC

Link to post
Share on other sites

Ray and everyone:

Although I am very much a beginner in the field of Arduino use and programming (and a little knowledge is a dangerous thing...), I have, in trying to understand the Accelstepper functions, begun to wonder whether it is possible to omit the sensor altogether by incorporating a facility into the code of a sketch to enable any required fine adjustment of the alignment of the turntable to be made by eye.

The sketch below includes such a facility (as the default action in the switch case function in the void loop() section) to move the turntable one step at a time for final adjustment as defined by the variable a. In order to function a pushbutton would also need to be provided (instead of the sensor) and an appropriate "while" statement included. A delay is provided between each step (or steps if a is set >1) which could be set to give a user a sufficient interval to precisely align the track using a digitalRead function allied to a pushbutton.

You will see that the sketch does not, at present, include a DCC library call because I don't yet understand how to use it. However it seems to me that the code might be usable for turntable control on analogue layouts. As set in this example the six cases following the switch case statement allow:

1. A full revolution forward (3200 steps)

2. A half revolution forward (1600 steps)

3. A quarter revolution forward (800) steps

4. A full revolution backward (-3200) steps

5. A half revolution backward (-1600) steps

6 A quarter revolution backward (-800) steps

as set by the value entered for variable i. For an analogue layout the value of i could be set manually by incorporating a potentiometer or selector switch combined with an analogueRead facility.

I feel sure that this would work for my own layout where I have a turntable with just two roads. However it should be possible to add additional roads by including extra cases in the switch case facility.
 

/* This test sketch (which is still under development) is for the Arduino Uno
   micro controller and illustrates how a model railway turntable may be
   rotated between defined positions (roads) using a stepper motor. It has been
   adapted by CC from example sketches in the Adadfruit stepper motor and
   Accelstepper libraries.  The ideas and inspiration behind this sketch came 
   from the thread:
   http://www.rmweb.co.uk/community/index.php?/topic/78578-dcc-controlled-peco-turntable-project/
   started by Tender on 12-Nov-2013 */

//  The Stepper Motor is directly  connected to the turntable shaft
//  (in this example a Mercury Motor SM42BY0011-25 was used).
//  This sketch requires the Adafruit_Motorshield v2 library - downloadable from
//  https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
//  and the AccelStepper library with AFMotor support 
//  https://github.com/adafruit/AccelStepper

// The sketch will only work with Adafruit Motorshield v2. 
// for the Arduino Uno.  It not work with v1 shields

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep1() {  
  myStepper1->onestep(FORWARD, MICROSTEP); // sets the stepper at 3200 steps per revolution 
}
void backwardstep1() {  
  myStepper1->onestep(BACKWARD, MICROSTEP); // sets the stepper at 3200 steps per revolution
}

AccelStepper Astepper1(forwardstep1, backwardstep1); // use functions to step

void setup()
{  
   Serial.begin(9600);           // sets up Serial library at 9600 bps for a serial monitor connected to the Artduino
   Serial.println("Stepper test for turntable");  // prints the comment between the "" on the serial monitor when starting.
  
  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz

    Astepper1.setMaxSpeed(50.0); // sets the maximum rotational speed of the stepper in steps per second.  
                                 // (the value of 50 will rotate the turntable for a full revolution 
                                 // in about 3200/50 seconds (i.e around 64 seconds).
   
    Astepper1.setAcceleration(10.0);
    
}

void loop()
  {
   Astepper1.moveTo(0); 
    
  int i;  // The variable i is used to set the amount of rotation between the roads of the turntable
  i=2;    // according to the case below.  Here i is set at 2 and so will select case 2 below. This
          // will cause the routine to rotate the the turntable 1600 steps; i.e. half a revolution forwards. 
          // Because there are only 6 cases set up selected actions will only be activated if i
          // has a value of 1 to 6.  If it is ourside these values the DEFAULT action is taken governed 
          // by the value set in the variable a.  
          // (Note that more cases could be added to this sketch if required).
                   
   int a; // Variable a can be considered to be the  "nudge" variable.  It is used to fine tune the
   a=1;   // alignment of the turntable between roads by eye if a road is not properly aligned. 
          // A value of 1 will cause the turntable to turn one step (0.11 degree) at a time  
          // and will pause between steps for the interval set by the delay funnction in the 
          // default case. 
           
   Serial.println(i); //prints the selected value of i on the monitor
   Serial.println("starting loop"); //and lets you know it has started
 
 switch (i) {
    case 1:
      //rotates turntable one revolution (3200 steps) forward when i = 1
     Astepper1.moveTo(3200); 
     Astepper1.run();
     Astepper1.runToPosition();    
     break;

    case 2:
      //rotates turntable half revolution (1600 steps) forward when i = 2
       Astepper1.moveTo(1600);   
       Astepper1.run();
       Astepper1.runToPosition();    
       break;

    case 3:
     //rotates turntable a quarter revolution (800 steps) forward when i = 3
       Astepper1.moveTo(800); 
       Astepper1.run();
       Astepper1.runToPosition();    
       break;

     case 4:
       //rotates turntable one revolution (3200 steps) backward when i = 4
       Astepper1.moveTo(-3200); 
       Astepper1.run();
       Astepper1.runToPosition();    
       break;

   case 5:
       //rotates turntable one revolution (1600 steps) backward when i = 5
       Astepper1.moveTo(-1600); 
       Astepper1.run();
       Astepper1.runToPosition();    
       break;

     case 6:
       //rotates turntable quarter revolution (800 steps) backward when i = 6
       Astepper1.moveTo(-800); 
       Astepper1.run();
       Astepper1.runToPosition();    
       break;     
       
     default: 
       Serial.println(a);  // prints the number of steps selected 
                           // (A negative value of a will cause
                           // the turntable to move in the opposite
                           // direction)  
                           
       Astepper1.move(0); 
       Serial.println("starting default case");
       delay(100); // delay between steps in millisecond
       Astepper1.move(a); 
       Astepper1.run(); 
      

I would be grateful for any observations/comments/suggestions/advice you might care to give

 

Regards

CC

Link to post
Share on other sites

I'm not sure if this is relevant to your comments but, here goes ...

 

The purpose of the sensor is not only to provide precise alignment but also to enable the software to detect when the turntable is back at the Zero position in the event that the position gets lost. For example when the system is started there is no other way for the software to know whether someone moved the turntable by hand.

 

And once there is a functioning Zero position detector it can also be used to eliminate the need for manual positioning.

 

...R

Edited by Robin2
Link to post
Share on other sites

Many thanks Robin for your very valid observations.

 

Of course the system described would require an appropriate reference position in lieu of a sensor.   However this could be the accurately aligned position of a designated turntable road (say road 1).   The positions of the other roads of the turntable would be defined relative to this reference by the number of steps needed to move the table to each individual road from road 1 (as set by the appropriate step values in the switch case commands).   Tender uses the same technique in his sketch with the sensor for the 7 road turntable. 

 

Once the turntable has moved to a selected road it can then be rotated 180 degrees to turn a locomotive using the subroutine shown in case 2 of my sketch.   The sketch could (hopefully relatively easily) be refined to enable movement backwards and forwards between individual roads (and not only from the reference road).

 

Just as with Tender's sketch care would be needed when setting up the system in the first place to ensure that the reference (in this case road1 or in Tender's case the sensor) was precisely aligned and that the number of steps in the moveTo functions were correct.   Precise alignment of the track in road1 would be helped by the "nudge" facility provided in the default case of my sketch.  The nudge facility would also be useful if ever road1 (or indeed any other road) got out of alignment or if someone moved the table by hand.

 

And to round off here is a picture of my test bed.   At the moment the table is missing its wheels and is secured to the spindle of the stepper motor with Bluetack...but it works.   I might even have a go a videoing it for a future post.

 

 

Best wishes

 

CC

post-12469-0-54926800-1397329123.jpg

Link to post
Share on other sites

 Precise alignment of the track in road1 would be helped by 

CC

Nice Picture - looking forward to the movie.

 

I would be inclined to install the tracks so that they align with convenient motor steps.

 

...R

Link to post
Share on other sites

Hi CC

No reason to go down the route you propose without a sensor, however every time you power up you will probably have to do a first initial alignment using your 'nudge' routine to ensure the turntable has a known starting point. This is done automatically on power up in my routine using the sensor.

Ray.

Link to post
Share on other sites

And here is the video:

 

https://www.youtube.com/watch?v=XsKdb_QYrfg

 

The video was produced from a single take of what happened as I videoed the turntable and is uncut - so it comes with warts and all.   The delay between movements is largely due to the fact that I am changing the values of the variables on the computer and then uploading the sketch to the Arduino.  Once I get the hang of the DCC aspects of Tender's sketch it would not be necessary to do this although I will still need to select the appropriate road from the computer with a DCC entry.

 

You will mnote that sometimes (though not always) when changing the direction of rotation there is a jerk as the table starts.   I am baffled as to why this happens.

 

As regards the initial alignment on power up....one could always leave the table aligned with the selected reference road before powering down.   Alternatively you could always position it by hand (as was done prototypically at Pwllheli!) and then fine tune the alignment with the nudge facility.

 

CC

  • Like 1
Link to post
Share on other sites

If you are uploading a revised sketch between each move that could account for the jerks. Until you can run several positions within the same sketch I doubt if there is any value trying to resolve the issue.

 

It should be easy to add some code to the sketch to take a character (0-9) from the Arduino IDE to select the different movements. Something like the following. I'm assuming tableMove is a global variable that is the value used in the statements that select the movement. Substitute your own variable name.

byte tableMove = 0;



void getValueFromIDE() {

   if (Serial.available > 0) {

      byte inByte = Serial.read();

      inByte = inByte - '0'; // converts a number character to a number

      if (inByte >= 0 && inByte <= 9) {

          tableMove = inByte;

      {

  }

}

...R

Link to post
Share on other sites

Really enjoying this series of articles on turntable control. I will be having ago myself in the near further

but cannot seem to be able to source the stepper motor. Where can I get hold of the stepper motor that cambriancoaster

and Tender are using.

 

Regards

 

Alan

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...