Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed QEI HIDScope biquadFilter MODSERIAL FXOS8700Q FastPWM
main.cpp@10:e1eb73e19540, 2019-09-20 (annotated)
- Committer:
- MatthewMaat
- Date:
- Fri Sep 20 09:47:41 2019 +0000
- Revision:
- 10:e1eb73e19540
- Parent:
- 9:ae548d05ec71
- Child:
- 11:de4a85703169
pwm test 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobertoO | 0:67c50348f842 | 1 | #include "mbed.h" |
MatthewMaat | 8:ec3c634390c7 | 2 | #include "HIDScope.h" |
MatthewMaat | 8:ec3c634390c7 | 3 | #include "QEI.h" |
RobertoO | 1:b862262a9d14 | 4 | #include "MODSERIAL.h" |
RobertoO | 0:67c50348f842 | 5 | //#include "BiQuad.h" |
MatthewMaat | 8:ec3c634390c7 | 6 | #include "FastPWM.h" |
MatthewMaat | 2:626688c21b6f | 7 | #include <iostream> |
MatthewMaat | 10:e1eb73e19540 | 8 | //#include "encoder.h" |
MatthewMaat | 5:cee5f898b350 | 9 | MODSERIAL pc(USBTX, USBRX); |
MatthewMaat | 10:e1eb73e19540 | 10 | //Encoder motor1(D13,D12); |
MatthewMaat | 8:ec3c634390c7 | 11 | PwmOut led(D9); |
MatthewMaat | 8:ec3c634390c7 | 12 | HIDScope scope(3); |
MatthewMaat | 10:e1eb73e19540 | 13 | AnalogIn ain(A0); |
MatthewMaat | 7:a32766b96d91 | 14 | |
MatthewMaat | 10:e1eb73e19540 | 15 | Ticker printstuff; |
MatthewMaat | 10:e1eb73e19540 | 16 | |
MatthewMaat | 10:e1eb73e19540 | 17 | void stuffz(void) |
MatthewMaat | 10:e1eb73e19540 | 18 | { |
MatthewMaat | 10:e1eb73e19540 | 19 | pc.printf("%f",ain*100.0f); |
MatthewMaat | 10:e1eb73e19540 | 20 | } |
MatthewMaat | 8:ec3c634390c7 | 21 | |
MatthewMaat | 8:ec3c634390c7 | 22 | |
MatthewMaat | 8:ec3c634390c7 | 23 | int main() |
MatthewMaat | 4:f988679bf9a1 | 24 | { |
MatthewMaat | 9:ae548d05ec71 | 25 | /*const float degrees_per_count = 90/200.0; //quick approach |
MatthewMaat | 8:ec3c634390c7 | 26 | const float ain_degrees_range = 90; //degrees over which the potmeter can control. |
MatthewMaat | 8:ec3c634390c7 | 27 | const float kp_1_range = -1; |
MatthewMaat | 8:ec3c634390c7 | 28 | AnalogIn setpoint_1(A0); |
MatthewMaat | 8:ec3c634390c7 | 29 | AnalogIn p_value(A1); |
MatthewMaat | 8:ec3c634390c7 | 30 | DigitalOut direction_1(D4); |
MatthewMaat | 8:ec3c634390c7 | 31 | PwmOut pwm_1(D5); |
MatthewMaat | 8:ec3c634390c7 | 32 | float degrees_setpoint; |
MatthewMaat | 8:ec3c634390c7 | 33 | float error; |
MatthewMaat | 8:ec3c634390c7 | 34 | float output; |
MatthewMaat | 8:ec3c634390c7 | 35 | pwm_1.period(1.0/10000); //10kHz |
MatthewMaat | 7:a32766b96d91 | 36 | |
MatthewMaat | 8:ec3c634390c7 | 37 | while (true) { |
MatthewMaat | 8:ec3c634390c7 | 38 | degrees_setpoint = setpoint_1 * ain_degrees_range; |
MatthewMaat | 8:ec3c634390c7 | 39 | error = degrees_setpoint-(motor1.getPosition()*degrees_per_count); //error = setpoint - current |
MatthewMaat | 8:ec3c634390c7 | 40 | output = error*p_value*kp_1_range; |
MatthewMaat | 8:ec3c634390c7 | 41 | if(output > 0) |
MatthewMaat | 8:ec3c634390c7 | 42 | { |
MatthewMaat | 8:ec3c634390c7 | 43 | direction_1.write(true); |
MatthewMaat | 8:ec3c634390c7 | 44 | } |
MatthewMaat | 8:ec3c634390c7 | 45 | else |
MatthewMaat | 8:ec3c634390c7 | 46 | { |
MatthewMaat | 8:ec3c634390c7 | 47 | direction_1.write(false); |
MatthewMaat | 8:ec3c634390c7 | 48 | } |
MatthewMaat | 8:ec3c634390c7 | 49 | pwm_1.write(fabs(output)); |
MatthewMaat | 8:ec3c634390c7 | 50 | scope.set(0,motor1.getPosition()); |
MatthewMaat | 8:ec3c634390c7 | 51 | scope.set(1,error); |
MatthewMaat | 8:ec3c634390c7 | 52 | scope.set(2,fabs(output)); |
MatthewMaat | 8:ec3c634390c7 | 53 | led.write(motor1.getPosition()/100.0); |
MatthewMaat | 8:ec3c634390c7 | 54 | scope.send(); |
MatthewMaat | 8:ec3c634390c7 | 55 | wait(0.01); |
MatthewMaat | 4:f988679bf9a1 | 56 | } |
MatthewMaat | 9:ae548d05ec71 | 57 | */ |
MatthewMaat | 10:e1eb73e19540 | 58 | pc.baud() |
MatthewMaat | 10:e1eb73e19540 | 59 | pc.printf("Starting....\r\n"); |
MatthewMaat | 10:e1eb73e19540 | 60 | printstuff.attach(stuffz,1); |
MatthewMaat | 9:ae548d05ec71 | 61 | led.write(ain); |
MatthewMaat | 4:f988679bf9a1 | 62 | } |