BrickUp API Service for Docker version.

FR_SVBServo.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Channels are locked to these pins:
  2. // Ch0=Pin2, Ch1=Pin3, Ch2=Pin4, Ch3=Pin5, Ch4=Pin6, Ch5=Pin7, Ch6=Pin8, Ch7=Pin9, Ch8=Pin10, Ch9=Pin11
  3. // Ch10=Pin12, Ch11=Pin13, Ch12=PinA0, Ch13=PinA1, Ch14=PinA2, Ch15=PinA3, Ch16=PinA4, Ch17=PinA5, Ch18=Pin0, Ch19=Pin1
  4. //
  5. // Serial commands:
  6. // # = Servo channel
  7. // P = Pulse width in us
  8. // p = Pulse width in 1/16 us
  9. // S = Speed in us per second
  10. // s = Speed in 1/16 us per second
  11. // T = Time in ms
  12. // PO = Pulse offset in us. -2500 to 2500 in us. Used to trim servo position.
  13. // Po = Pulse offset in 1/16us -40000 to 40000 in 1/16 us
  14. // I = Invert servo movements.
  15. // N = Non-invert servo movements.
  16. // Q = Query movement. Return "." if no servo moves and "+" if there are any servos moving.
  17. // QP = Query servo pulse width. Return 20 bytes where each is from 50 to 250 in 10us resolution.
  18. // So you need to multiply byte by 10 to get pulse width in us. First byte is servo 0 and last byte is servo 20.
  19. // <cr> = Carrage return. ASCII value 13. Used to end command.
  20. //
  21. // Examples:
  22. // #0 P1500 T1000<cr> - Move Servo 0 to 1500us in 1 second.
  23. // #0 p24000 T1000<cr> - Move Servo 0 to 1500us in 1 second.
  24. // #0 p40000 s1600<cr> - Move Servo 0 to 2500us in 100us/s speed
  25. // #0 p40000 S100<cr> - Move Servo 0 to 2500us in 100us/s speed
  26. // #0 P1000 #1 P2000 T2000<cr> - Move Servo 0 and servo at the samt time from current pos to 1000us and 2000us in 2 second.
  27. // #0 P2400 S100<cr> - Move servo 0 to 2400us at speed 100us/s
  28. // #0 P1000 #1 P1200 S500 #2 P1400 T1000<cr> - Move servo 0, 1 and 2 at the same time, but the one that takes longes S500 or T1000 will be used.
  29. // #0 PO100 #1 PO-100<cr> - Will set 100 us offset to servo 0 and -100 us ofset to servo 1
  30. // #0 Po1600 #1 Po-1600<cr> - Will set 100 us offset to servo 0 and -100 us ofset to servo 1
  31. // #0 I<cr> - Will set servo 0 to move inverted from standard
  32. // #0 N<cr> - Will set servo 0 back to move non-inverted
  33. // Q<cr> - Will return "." if no servo moves and "+" if there are any servos moving
  34. // QP<cr> - Will retur 18 bytes (each 20ms apart) for position of servos 0 to 17
  35. #include "FR_SVBServo.h"
  36. FR_SVBServo::FR_SVBServo(SoftwareSerial* toServo)
  37. {
  38. _toServo=toServo;
  39. lowerPW=520;
  40. upperPW=2480;
  41. init();
  42. }
  43. FR_SVBServo::FR_SVBServo(SoftwareSerial* toServo,int minPW,int maxPW)
  44. {
  45. _toServo=toServo;
  46. lowerPW=minPW;
  47. upperPW=maxPW;
  48. init();
  49. }
  50. void FR_SVBServo::init()
  51. {
  52. // _toServo=SoftwareSerial(7,8); // RX, TX
  53. _toServo->begin(115200);
  54. }
  55. boolean FR_SVBServo::servoActive()
  56. {
  57. _toServo->write(81);
  58. _toServo->write(13);
  59. while(_toServo->available()==0);
  60. // Serial.println("W");
  61. if(_toServo->read()=='.')
  62. return false;
  63. return true;
  64. }
  65. void FR_SVBServo::moveServo(byte num,byte degree,int stime)
  66. {
  67. if(degree!=oldDegree[num])
  68. {
  69. char msg[20];
  70. sprintf(msg,"#%d P%d T%d",servo2ch[num],degree2time(degree),stime);
  71. _toServo->write(msg);
  72. _toServo->write(13);
  73. oldDegree[num]=degree;
  74. // Serial.println(msg);
  75. }
  76. }
  77. void FR_SVBServo::moveServoArray(byte *degree,int stime,byte starti,byte endi)
  78. {
  79. String temp;
  80. char tmp[20];
  81. byte first=1;
  82. for(int i=starti;i<=endi;i++)
  83. {
  84. if(degree[i]!=oldDegree[i])
  85. {
  86. if(first!=1) temp+=" ";
  87. else first=0;
  88. sprintf(tmp,"#%d P%d",servo2ch[i],degree2time(degree[i]));
  89. temp+=tmp;
  90. oldDegree[i]=degree[i];
  91. }
  92. }
  93. char msg[temp.length()];
  94. for(int i=0;i<temp.length();i++)
  95. msg[i]=temp.charAt(i);
  96. sprintf(tmp," T%d",stime);
  97. _toServo->write(msg);
  98. _toServo->write(tmp);
  99. _toServo->write(13);
  100. // Serial.println(msg);
  101. }
  102. byte FR_SVBServo::readDegree(byte num)
  103. {
  104. _toServo->write("QP");
  105. _toServo->write(13);
  106. while(_toServo->available()>=18);
  107. // Serial.println("W");
  108. for(int i=0;i<num-1;i++)
  109. _toServo->read();
  110. return _toServo->read();
  111. }
  112. void FR_SVBServo::readAllDegree(byte *retAngle)
  113. {
  114. _toServo->write("QP");
  115. _toServo->write(13);
  116. while(_toServo->available()>=18);
  117. // Serial.println("W");
  118. for(int i=0;i<18;i++)
  119. retAngle[i]=_toServo->read();
  120. }
  121. int FR_SVBServo::degree2time(byte degree)
  122. {
  123. return map(degree,0,180,lowerPW,upperPW);
  124. }