Advertisement
Tempist

stspin6470 [.ino]

Sep 30th, 2019
1,066
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.91 KB | None | 0 0
  1. /****************************************************************************
  2.  * STspin6470.ino
  3.  * Two AXIS example for Autodriver WITH stm X-NUCLEO-IHM02A1 BOARD (2x L6470 Stepper drivers)
  4.  * and Astrosyn MY5401 stepper motor
  5.  *
  6.  * This file demonstrates the use of two Autodriver boards in one sketch, to
  7.  * control a five-axis gantry system such as one might find on a 3D printer.
  8.  *
  9.  * @WizardYawns
  10.  * ****************************************************************************/
  11.  
  12. #include <AutoDriver.h>
  13. #include <SPI.h>
  14.  
  15. #define NUM_BOARDS 2
  16.  
  17. // 2 drivers are on the X-NUCLEO-IHM02A1 BOARD
  18. AutoDriver YAxis(0, A2, 4);  // Nr, CS, Reset => 0 , D16/A2 (PA4), D4 (PB5) for IHM02A1 board
  19. AutoDriver XAxis(1, A2, 4);  // Nr, CS, Reset => 1 , D16/A2 (PA4), D4 (PB5) for IHM02A1 board
  20. unsigned int xspeed = 600;
  21. unsigned int yspeed = 600;
  22.  
  23. // It may be useful to index over the drivers, say, to set
  24. //  the values that should be all the same across all the
  25. //  boards. Let's make an array of pointers to AutoDriver
  26. //  objects so we can do just that.
  27. AutoDriver *boardIndex[NUM_BOARDS];
  28.  
  29. void setup()
  30. {
  31.   Serial.begin(115200);
  32.   Serial.println("Hello world!");
  33.  
  34. // Start by setting up the SPI port and pins. The Autodriver library does not do this for you!
  35.   pinMode(3, INPUT);     // D3 = SPI SCK, wired-routed to D13(SCK), (D3 setup as input - tri-state)
  36.   pinMode(4, OUTPUT);    // D4 = nReset
  37.   pinMode(MOSI, OUTPUT); // Serial IN
  38.   pinMode(MISO, INPUT);  // Serial OUT
  39.   pinMode(13, OUTPUT);   // serial clock -> routed to D3 by wire
  40.   pinMode(A2, OUTPUT);   // CS signal
  41.   digitalWrite(A2, HIGH);   // nCS set High
  42.   digitalWrite(4, LOW);     // toggle nReset
  43.   delay(100);
  44.   digitalWrite(4, HIGH);
  45.   SPI.begin();             // start
  46.  
  47.   // Set the pointers in our global AutoDriver array to
  48.   //  the objects we've created.
  49.   boardIndex[1] = &YAxis;
  50.   boardIndex[0] = &XAxis;
  51.  
  52.   // Configure the boards to the settings we wish to use
  53.   //  for them. See below for more information about the
  54.   //  settings applied.
  55.   spi_test();
  56.   configureBoards();
  57.  
  58. }
  59.  
  60. // loop() is going to wait to receive a character from the
  61. //  host, then do something based on that character.
  62. void loop()
  63. {
  64.   char rxChar = 0;
  65.   if (Serial.available())
  66.   {
  67.     rxChar = Serial.read();
  68.     // This switch statement handles the various cases that may come from the host via the serial port.
  69.     //  Do note that not all terminal programs will encode e.g. Page Up as 0x0B. I'm using CoolTerm
  70.     //  in Windows and this is what CoolTerm sends when I strike these respective keys.
  71.     //  only Yaxis control (one motor) is implemented here as example
  72.     switch(rxChar)
  73.     {
  74.       case 'x':  // x Arrow : speed 0
  75.         YAxis.run(0,0);
  76.         break;
  77.       case 'd':  // right Arrow:  turn right
  78.         YAxis.run(1,yspeed);
  79.         break;
  80.       case 'a':  // left Arrow:  turn left
  81.         YAxis.run(0,yspeed);
  82.         break;
  83.       case 'w':  // Up Arrow : speed up 10
  84.         yspeed = constrain(yspeed+10, 25, 1500);
  85.         Serial.print("Spd:");Serial.println(yspeed);
  86.         break;
  87.       case 's':  // Down Arrow : speed down 10
  88.         yspeed = constrain(yspeed-10, 25, 1500);
  89.         Serial.print("Spd:");Serial.println(yspeed);
  90.         break;
  91.       case 'g':
  92.         getBoardStatus(1);
  93.         break;
  94.       case 'h':
  95.         getBoardConfigurations(1);
  96.         break;
  97.       case 'r':
  98.           YAxis.resetDev();
  99.           XAxis.resetDev();  
  100.         break;          
  101.       case 'c':
  102.         break;
  103.       default:
  104.         Serial.println(".");
  105.     }
  106.   }
  107. }
  108.  
  109.  
  110. // Test Serial chain: shift out ox55 pattern and read back : should be 2 for  X-NUCLEO-IHM02A1 BOARD
  111. // Important: L6470 Serial SPI only works if Vcc (Chip voltage) AND Vss (motor Voltage) is applied!
  112. byte spi_test()
  113. {
  114.   byte val,t,u=0;
  115.   digitalWrite(A2, LOW);
  116.   SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  117.   Serial.print("\n SPI test: ");
  118.   for(t=0x55;t<0x6D;++t)
  119.   {
  120.   val=SPI.transfer(t);
  121.   Serial.print(" Tx:"); Serial.print(t,HEX);Serial.print("/Rx:"); Serial.print(val,HEX);
  122.   if (val==0x55) {u=t-0x55;t=0x6D;}
  123.   delay(100);  
  124.   }
  125.   Serial.print("\n Chain="); Serial.println(u);Serial.print("\n");
  126.   return(u);
  127. }
  128.  
  129.  
  130. //  For ease of reading, we're just going to configure all the boards to the same settings.
  131. void configureBoards()
  132. {
  133.   int paramValue;
  134.   Serial.println("Configuring boards...");
  135.   for (int i = 0; i < NUM_BOARDS; i++)
  136.   {
  137.     // tell each device which SPI port is being used
  138.     boardIndex[i]->SPIPortConnect(&SPI);
  139.  
  140.     // reset device //
  141.     boardIndex[i]->resetDev();
  142.  
  143.     // Set the Overcurrent Threshold to 6A(max). The OC detect circuit is quite sensitive; even if the current is only momentarily
  144.     //  exceeded during acceleration or deceleration, the driver will shutdown. This is a per channel value; it's useful to
  145.     //  consider worst case, which is startup.
  146.     // These MY5401 motors have a 4.3 ohm static winding resistance; at 12V, the current is est 3A at start
  147.     // so we need to use the KVAL settings (see below) to trim that value back to a safe level. >>3A
  148.     boardIndex[i]->setOCThreshold(OCD_TH_6000mA);
  149.    
  150.     // KVAL is a modifier that sets the effective voltage applied to the motor. KVAL/255 * Vsupply = effective motor voltage.
  151.     //  This lets us hammer the motor harder during some phases  than others, and to use a higher voltage to achieve better
  152.     //  torqure performance even if a motor isn't rated for such a high current.
  153.     // This IHM02A1 BOARD has 12V motors and a 12V supply.
  154.    boardIndex[i]->setRunKVAL(128);  // 128/255 * 12V = 6V
  155.    boardIndex[i]->setAccKVAL(128);  // 192/255 * 12V = 6V
  156.    boardIndex[i]->setDecKVAL(64);  // 128/255 * 12V = 3V
  157.    boardIndex[i]->setHoldKVAL(32);  // 32/255 * 12V = 1.5V  // low voltage, almost free turn
  158.  
  159.    // The dSPIN chip supports microstepping for a smoother ride. This function provides an easy front end for changing the microstepping mode.
  160.    // once in full speed, it will step up to half-step
  161.     boardIndex[i]->configStepMode(STEP_FS_64);
  162.    
  163.     // When a move command is issued, max speed is the speed the Motor tops out at while completing the move, in steps/s
  164.     boardIndex[i]->setMaxSpeed(1500);  
  165.     boardIndex[i]->setMinSpeed(50);
  166.    
  167.     // Acceleration and deceleration in steps/s/s. Increasing this value makes the motor reach its full speed more quickly, at the cost of possibly causing it to slip and miss steps.
  168.     boardIndex[i]->setAcc(500);
  169.     boardIndex[i]->setDec(500);
  170.    
  171.  
  172.  
  173.   }
  174. }
  175.  
  176. // Reads back the "config" register from board [i] in the series.
  177. //  This should be 0x2e88 after a reset of the Autodriver board, or of the control board (as it resets the Autodriver at startup).
  178. //  A reading of 0x0000 means something is wrong with your hardware.
  179. int getBoardConfigurations(int i)
  180. {
  181.   int paramValue;
  182.   Serial.print("\n Board configurations: ");
  183.  
  184.     // It's nice to know if the board is connected okay. We can read
  185.     //  the config register and print the result; it should be 0x2e88
  186.     //  on startup.
  187.     paramValue = boardIndex[i]->getParam(CONFIG);
  188.     Serial.print(" Config:");
  189.     Serial.println(paramValue, HEX);
  190. return(paramValue);
  191. }
  192.  
  193. // Reads back the "status" register from board [i] in the series.
  194.  
  195. int getBoardStatus(int i)
  196. {
  197.   int paramValue;
  198.   Serial.print("\n Status ");
  199.  
  200.     // See Datasheet page 55 : Status register
  201.     // MOT_STAT: 00 = stopped / 01=Acc  / 10=Dec / 11 = const speed
  202.     // OCD = Over current Flag (Active low)
  203.     paramValue = boardIndex[i]->getStatus();
  204.     Serial.print("[hex]"); Serial.println(paramValue, HEX);
  205.     Serial.println("0-HIZ | 1-BUSY | 2-SW_F | 3-SW_EVN | 4-DIR |  5-MOT_STAT  | NOPR_CMD");
  206.     Serial.print("   ");Serial.print(paramValue&0x01);
  207.     Serial.print("  |  "); Serial.print((paramValue>>1)&0x01);
  208.     Serial.print("     |  "); Serial.print((paramValue>>2)&0x01);
  209.     Serial.print("     |  "); Serial.print((paramValue>>3)&0x01);
  210.     Serial.print("       |  "); Serial.print((paramValue>>4)&0x01);
  211.     Serial.print("    |    "); Serial.print((paramValue>>5)&0x03);
  212.     Serial.print("         |  "); Serial.println((paramValue>>7)&0x01);
  213.     Serial.println("-------------------------------------------------------------------");    
  214.     Serial.println("8-WRNG_CMD | 9-UVLO | 10-TH_WRN | 11-TH_SD | 12-OCD | 13-STLOS_A | 14-STLOS_B | 15-SCK_MOD");
  215.     Serial.print("   "); Serial.print((paramValue>>8)&0x01);
  216.     Serial.print("       |  "); Serial.print((paramValue>>9)&0x01);
  217.     Serial.print("     |  "); Serial.print((paramValue>>10)&0x01);
  218.     Serial.print("        |  "); Serial.print((paramValue>>11)&0x01);
  219.     Serial.print("       |  "); Serial.print((paramValue>>12)&0x01);
  220.     Serial.print("     |  "); Serial.print((paramValue>>13)&0x01);
  221.     Serial.print("         |  "); Serial.print((paramValue>>14)&0x01);
  222.     Serial.print("         |  "); Serial.println((paramValue>>15)&0x01);
  223.  
  224. return(paramValue);
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement