Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.75 KB | None | 0 0
  1. // "Arduino UNO",
  2. // a 3-phase brushless motor with 14-pole and 12-slot,
  3. // two gyro modules with "L3GD20" chips, and
  4. // a motor driver IC "L298"
  5. // information:
  6. // The gyro which CS pin is connected to digital pin 8 is attached to the base of motor.
  7. // The other gyro which CS pin is connected to digital pin 9 is attached to top of motor.
  8. // Eight AA-batteries for 12V would work better than 6V with four batteries.
  9. // Don't touch gyros for 8 seconds after reset Arduino.
  10. // Three wires from the motor are connected to digital pins 3, 5 and 6 appropriately.
  11. // If the motor rotates wrong direction, the connection of these 3 wires should be changed.
  12. // If the motor does not stop at start, two alternative lines in "void chkAndCtl()" should be used instead of line A and B.
  13. #include <SPI.h>
  14. long input = 0;
  15. long recOmegaD = 0;
  16. long omegaD = 0;
  17. long thetaM = 0;
  18. long recMicros = 0;
  19. long recOmegaDa = 0;
  20. long omegaDa = 0;
  21. long thetaMa = 0;
  22. long deg1000 = 0;
  23. int variPwm = 0;
  24. byte phase = 0;
  25. int rz, rza, dRz, dRza;
  26. long R, Ra;
  27. const int motorPin1 = 3;
  28. const int motorPin2 = 5;
  29. const int motorPin3 = 6;
  30. const long L1000 = 51429;
  31. const int nLnT1000 = 2140;
  32. const int LnT1000 = 4291;
  33. const int LnLvl10 = 390;
  34. const long slope1000 = 41245;
  35. void L3GD20_write(byte reg, byte val) {
  36.  digitalWrite(8, LOW);
  37.  SPI.transfer(reg);
  38.  SPI.transfer(val);
  39.  digitalWrite(8, HIGH);
  40. }
  41. byte L3GD20_read(byte reg) {
  42.  byte ret = 0;
  43.  digitalWrite(8, LOW);
  44.  SPI.transfer(reg | 0x80);
  45.  ret = SPI.transfer(0);
  46.  digitalWrite(8, HIGH);
  47.  return ret;
  48. }
  49. void L3GD20a_write(byte reg, byte val) {
  50.  digitalWrite(9, LOW);
  51.  SPI.transfer(reg);
  52.  SPI.transfer(val);
  53.  digitalWrite(9, HIGH);
  54. }
  55. byte L3GD20a_read(byte reg) {
  56.  byte ret = 0;
  57.  digitalWrite(9, LOW);
  58.  SPI.transfer(reg | 0x80);
  59.  ret = SPI.transfer(0);
  60.  digitalWrite(9, HIGH);
  61.  return ret;
  62. }
  63. void setup () {
  64.  pinMode(motorPin1, OUTPUT);
  65.  pinMode(motorPin2, OUTPUT);
  66.  pinMode(motorPin3, OUTPUT);
  67.  pinMode(8, OUTPUT);
  68.  pinMode(9, OUTPUT);
  69.  digitalWrite(8, HIGH);
  70.  digitalWrite(9, HIGH);
  71.  SPI.begin();
  72.  SPI.setBitOrder(MSBFIRST);
  73.  SPI.setDataMode(SPI_MODE3);
  74.  SPI.setClockDivider(SPI_CLOCK_DIV2);
  75.  L3GD20_write(0x20, B11001111);
  76.  L3GD20_write(0x23, B00000000);
  77.  L3GD20a_write(0x20, B11001111);
  78.  L3GD20a_write(0x23, B00000000);
  79.  //calibrate();
  80.  Serial.begin(57600);
  81.  delay(50);
  82.  recMicros = micros();
  83. }
  84. void loop (){
  85. // chkAndCtl();
  86. // calcPwms();
  87. if ( Serial.available() > 0 ) {
  88. delay(1);
  89. input = serialReadAsInt() * 100; //1000 times degrees to rotate
  90. }
  91. if ( input>deg1000 ) {
  92. if ( input-deg1000>200 ) { deg1000=deg1000+200; }
  93. else { deg1000=input; }
  94. } else {
  95. if ( input-deg1000<-200 ) { deg1000=deg1000-200; }
  96. else { deg1000=input; }
  97. }
  98. calcPwms();
  99.  if ( phase == 1 ) {
  100.  analogWrite(motorPin1, variPwm);
  101.  analogWrite(motorPin2, 255);
  102.  analogWrite(motorPin3, 0);
  103.  }
  104.  if ( phase == 2 ) {
  105.  analogWrite(motorPin1, 255);
  106.  analogWrite(motorPin2, variPwm);
  107.  analogWrite(motorPin3, 0);
  108.  }
  109.  if ( phase == 3 ) {
  110.  analogWrite(motorPin1, 255);
  111.  analogWrite(motorPin2, 0);
  112.  analogWrite(motorPin3, variPwm);
  113.  }
  114.  if ( phase == 4 ) {
  115.  analogWrite(motorPin1, variPwm);
  116.  analogWrite(motorPin2, 0);
  117.  analogWrite(motorPin3, 255);
  118.  }
  119.  if ( phase == 5 ) {
  120.  analogWrite(motorPin1, 0);
  121.  analogWrite(motorPin2, variPwm);
  122.  analogWrite(motorPin3, 255);
  123.  }
  124.  if ( phase == 6 ) {
  125.  analogWrite(motorPin1, 0);
  126.  analogWrite(motorPin2, 255);
  127.  analogWrite(motorPin3, variPwm);
  128.  }
  129. }
  130. long serialReadAsInt() {
  131. char c[ 6 ]; //ten times the degrees to rotate
  132. for ( int i = 0; i < 6; i++ ) {
  133. c[ i ] = Serial.read();
  134. if ( c[ i ] == '\0' )
  135. break;
  136. }
  137. return atol( c );
  138. }
  139. void calcPwms() {
  140.  int degPwm1000 = deg1000 % 8571;
  141.  if ( degPwm1000 < 0 ) { degPwm1000 = degPwm1000 + 8571; }
  142.  long linePwm = ( LnLvl10 + ( slope1000 * degPwm1000 ) / 100000 + 5 ) / 10;
  143.  long degNL1000 = abs( degPwm1000 - LnT1000 - nLnT1000 );
  144.  long nLinePwm = 255 - ( ( - 2935 * ( sq( sq(degNL1000)/1000 ) /1000 )
  145.  + 8413 * ( ( sq(degNL1000)/1000 ) * degNL1000 /1000 )
  146.  - 11421 * ( sq(degNL1000)/1000 )
  147.  + 32929 * ( degNL1000 )
  148.  ) / 100000 + 5
  149.  ) / 10;
  150.  int pwmShape = 0;
  151.  if ( degPwm1000 < LnT1000 ) { pwmShape = linePwm; } else { pwmShape = nLinePwm; }
  152.  int dDegPwm1000 = deg1000 % 17143;
  153.  if ( dDegPwm1000 < 0 ) { dDegPwm1000 = dDegPwm1000 + 17143; }
  154.  if ( dDegPwm1000 < 8571 ) { variPwm = 255 - pwmShape; } else { variPwm = pwmShape; }
  155.  long shftDeg1000 = ( deg1000 + L1000/2 + nLnT1000 ) % L1000;
  156.  if ( shftDeg1000 < 0 ) { shftDeg1000 = shftDeg1000 + L1000; }
  157.  phase = 0;
  158.  if ( shftDeg1000 < 8571 ) { phase = 1; } else {
  159.  if ( shftDeg1000 < 17143 ) { phase = 2; } else {
  160.  if ( shftDeg1000 < 25714 ) { phase = 3; } else {
  161.  if ( shftDeg1000 < 34286 ) { phase = 4; } else {
  162.  if ( shftDeg1000 < 42857 ) { phase = 5; } else {
  163.  phase = 6; }
  164.  }
  165.  }
  166.  }
  167.  }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement