Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. ////////////////////////////////
  2. /////
  3. ///// Circuit-version-2 nonlinar 2
  4. /////
  5. /////
  6. ////////////////////////////////
  7.  
  8. int torquePin=DAC0;
  9.  
  10. unsigned long lastTriggerMillis=0;
  11. int triggerIntervalMillis=1000;
  12.  
  13. boolean serial=true;
  14. boolean autonomous=true;
  15.  
  16. int rotationDirection=0;
  17.  
  18. //For intinal position
  19. int zeroTorque=2048;
  20. int zeroGravity=1735;
  21. int nextVelocityTicks = 0;
  22.  
  23. double stiffness=16.0/600.0;
  24.  
  25. int lastTorque=zeroTorque;
  26.  
  27. //veclocity calculation
  28. int calculatedVelocityTicks;
  29. int velocityLastPos;
  30. unsigned long lastVelocitySampleMillis;
  31. int velocitySampleTime=1;
  32.  
  33. double coulombFactor=0;
  34. double c1Factor=0;
  35. double c2Factor=0;
  36. double c3Factor=0;
  37. double cLinearFactor=0;
  38.  
  39.  
  40. //Sigmoid Curve Parameters
  41. double sigmoidScalingFactor=0.0007;
  42. double sigmoidAmplitude=500.0;
  43.  
  44. boolean overspeed=false;
  45.  
  46. int zeroPosition=0;
  47. int cpuPosition=0;
  48. int previousCpuPosition = 0;
  49. int positions[10] = {0};
  50. int memoryCounter = 0;
  51.  
  52. int error=0;
  53.  
  54. int torqueOverride=zeroTorque;
  55.  
  56. unsigned long cachedMillis;
  57.  
  58. #define forward 0
  59. #define reverse 1
  60.  
  61. void setup()
  62. {
  63. delay(1000);
  64. if(serial) Serial.begin(115200);
  65.  
  66. setupQam();
  67.  
  68. analogWriteResolution(12);
  69.  
  70. pinMode(torquePin, OUTPUT);
  71. analogWrite(torquePin, zeroTorque);
  72. }
  73.  
  74. void setupQam()
  75. {
  76. // Setup Quadrature Encoder with Marker
  77. REG_PMC_PCER0 = (1<<27); // activate clock for TC0
  78. REG_PMC_PCER0 = (1<<28); // activate clock for TC1
  79.  
  80. // select XC0 as clock source and set capture mode
  81. REG_TC0_CMR0 = 5;
  82.  
  83. // activate quadrature encoder and position measure mode, no filters
  84. REG_TC0_BMR = (1<<9)|(1<<8); //|(1<<12)
  85.  
  86. // select XC0 as clock source and set capture mode
  87. REG_TC0_CCR0 = (1<<2) | (1<<0) | (1<<14);
  88. }
  89.  
  90. void loop()
  91. {
  92. cachedMillis=millis();
  93. updatePosition();
  94.  
  95. calculateVelocity();
  96. applyTorque();
  97. previousCpuPosition = cpuPosition;
  98. if(serial) performOutput();
  99. if(serial) processSerialInput();
  100. }
  101.  
  102. void updatePosition()
  103. {
  104.  
  105. cpuPosition=((int)REG_TC0_CV0)-zeroPosition;
  106. rotationDirection=((REG_TC0_QISR>>8)&0x1);
  107.  
  108.  
  109. //error=(REG_TC0_QISR>>2)&0x1;
  110. }
  111.  
  112. //int sign(int x)
  113. //{
  114. // return x>0?1:(x<0?-1:0);
  115. //}
  116.  
  117. int sign(double x)
  118. {
  119. return x>0?1:(x<0?-1:0);
  120. }
  121.  
  122. void applyTorque()
  123. {
  124. //scale: 1/1
  125. int adjPos=cpuPosition;
  126.  
  127. double yDot=((double)calculatedVelocityTicks)/25;
  128. double y=adjPos;
  129.  
  130. double coulombComponent=0;
  131. //double linearComponent=y; //This is replaced by piecewiseLinearComponent
  132. double yComponent=0;
  133. double yDotComponent=0;
  134. double yDot2Component=0;
  135. double yDot3Component=0;
  136.  
  137. int torque=zeroTorque;
  138.  
  139. if(autonomous)
  140. {
  141. double yDot2=yDot*yDot;
  142. double yDot3=yDot2*yDot;
  143.  
  144. if(sign(yDot)>0) //down
  145. {
  146. coulombComponent=10.955;
  147. yDotComponent=30.28 * yDot;
  148. yDot2Component=-9.8495 * yDot2;
  149. yDot3Component=1.1143* yDot3;
  150. }
  151. else //up
  152. {
  153. coulombComponent=-10.265;
  154. yDotComponent=30.28 * yDot;
  155. yDot2Component=10.2495 * yDot2;
  156. yDot3Component=1.1143 * yDot3;
  157. }
  158.  
  159. double sigmoidComponent=0.0;
  160. sigmoidComponent=(1.0/(1+pow(M_E, -sigmoidScalingFactor*y)))*2.0-1.0;
  161.  
  162.  
  163. double output=
  164. -sigmoidComponent*sigmoidAmplitude
  165. +yDotComponent*(c1Factor-cLinearFactor) *5.64
  166. +yDot2Component*(c2Factor) *5.64
  167. +yDot3Component*(c3Factor) *5.64
  168. +coulombComponent*(coulombFactor) *5.64;
  169.  
  170.  
  171. torque=max(1.0, min(2048.0 + output, 4094.0));
  172. }
  173. else
  174. {
  175. torque=torqueOverride;
  176. }
  177.  
  178. //safety checks
  179. if(torque>=4000 || torque<=100 || cpuPosition>11000 || cpuPosition<-11500 || error !=0 || calculatedVelocityTicks>1900)
  180. {
  181. overspeed=true;
  182. }
  183.  
  184. //if(overspeed)
  185. //{
  186. // torque=zeroTorque;
  187. // }
  188.  
  189. //save us the extra write
  190. if(torque!=lastTorque)
  191. {
  192. analogWrite(torquePin, torque);
  193. lastTorque=torque;
  194. }
  195.  
  196. }
  197.  
  198. void calculateVelocity()
  199. {
  200. unsigned long currentTimeMillis=cachedMillis;
  201. if( abs(currentTimeMillis-lastVelocitySampleMillis) >= velocitySampleTime )
  202. {
  203. lastVelocitySampleMillis=currentTimeMillis;
  204. calculatedVelocityTicks=(cpuPosition-velocityLastPos);
  205.  
  206. velocityLastPos=cpuPosition;
  207.  
  208. //int cpuDelta = cpuPosition - previousCpuPosition;
  209. //nextVelocityTicks = calculatedVelocityTicks - 6 *cpuDelta*velocitySampleTime;
  210. //calculatedVelocityTicks = nextVelocityTicks*4; //should be checked !!!!
  211. }
  212.  
  213.  
  214. // unsigned long currentTimeMillis=cachedMillis;
  215. // if( abs(currentTimeMillis-lastVelocitySampleMillis) >velocitySampleTime )
  216. // {
  217. // lastVelocitySampleMillis=currentTimeMillis;
  218. // calculatedVelocityTicks=(cpuPosition-velocityLastPos);
  219. // velocityLastPos=cpuPosition;
  220. // }
  221.  
  222. }
  223.  
  224. void processSerialInput()
  225. {
  226. if(Serial.available()>0)
  227. {
  228. char x=Serial.read();
  229. if(x=='v')
  230. {
  231. int incomingDutyCycle = Serial.parseInt();
  232. autonomous=false;
  233. torqueOverride=incomingDutyCycle;
  234. }
  235. else if(x=='r')
  236. {
  237. zeroPosition=cpuPosition+zeroPosition;
  238. velocityLastPos=0;
  239. cpuPosition=0;
  240. torqueOverride=zeroTorque;
  241. autonomous=true;
  242. overspeed=false;
  243. }
  244. else if(x=='s')
  245. {
  246. double stiffnessIncoming=Serial.parseInt();
  247. stiffness = 16.0/stiffnessIncoming;
  248. }
  249. else if(x=='p')
  250. {
  251. int posOffset=Serial.parseInt();
  252. zeroPosition+=posOffset;
  253. }
  254. else if(x=='d')
  255. {
  256. c1Factor=getPercentageFromInput();
  257. }
  258. else if(x=='c')
  259. {
  260. coulombFactor=getPercentageFromInput();
  261. }
  262. else if(x=='q')
  263. {
  264. c2Factor=getPercentageFromInput();
  265. }
  266. else if(x=='b')
  267. {
  268. c3Factor=getPercentageFromInput();
  269. }
  270. else if(x=='l')
  271. {
  272. cLinearFactor=getPercentageFromInput();
  273.  
  274. }
  275. Serial.read(); //newline
  276. }
  277. }
  278.  
  279. double getPercentageFromInput()
  280. {
  281. int value=Serial.parseInt();
  282. double percentage=((double)value)/100.0;
  283. return percentage;
  284. }
  285.  
  286. int perfSpeedTicks=0;
  287. void performOutput()
  288. {
  289. perfSpeedTicks++;
  290. unsigned long currentMillis=cachedMillis;
  291. if((currentMillis-lastTriggerMillis)>=triggerIntervalMillis)
  292. {
  293. if(perfSpeedTicks<1000) overspeed=true;
  294. if(overspeed) Serial.print("!ERR ");
  295. Serial.print(cpuPosition);
  296. Serial.print(" T:");
  297. Serial.print(lastTorque);
  298. Serial.print(" D:");
  299. Serial.print(rotationDirection);
  300. Serial.print(" S:");
  301. Serial.print(calculatedVelocityTicks);
  302. Serial.print(" H:");
  303. Serial.print(perfSpeedTicks);
  304. Serial.print("\n");
  305. lastTriggerMillis=currentMillis;
  306. perfSpeedTicks=0;
  307. }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement