Data kecepatan motor thdp pwm Data kec motor thdp waktu
Dependencies: mbed encoderKRAI Motor_new
main.cpp@0:cddb02c09d3f, 2019-10-28 (annotated)
- Committer:
- dwianiyulia
- Date:
- Mon Oct 28 12:52:13 2019 +0000
- Revision:
- 0:cddb02c09d3f
Ambil Data Motor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dwianiyulia | 0:cddb02c09d3f | 1 | #include "mbed.h" |
dwianiyulia | 0:cddb02c09d3f | 2 | #include "Motor.h" |
dwianiyulia | 0:cddb02c09d3f | 3 | #include "encoderKRAI.h" |
dwianiyulia | 0:cddb02c09d3f | 4 | #include <stdio.h> |
dwianiyulia | 0:cddb02c09d3f | 5 | |
dwianiyulia | 0:cddb02c09d3f | 6 | Serial pc(USBTX, USBRX, 115200) |
dwianiyulia | 0:cddb02c09d3f | 7 | |
dwianiyulia | 0:cddb02c09d3f | 8 | //PinList |
dwianiyulia | 0:cddb02c09d3f | 9 | Motor motorA(PB_7,PC_3,PC_0); |
dwianiyulia | 0:cddb02c09d3f | 10 | EncoderKRAI encoderA(PB_1,PB_2,538,encoderKRAI::X4_ENCODING);// input pin |
dwianiyulia | 0:cddb02c09d3f | 11 | |
dwianiyulia | 0:cddb02c09d3f | 12 | //Define |
dwianiyulia | 0:cddb02c09d3f | 13 | double pulse, total_pulse,velA; |
dwianiyulia | 0:cddb02c09d3f | 14 | float pwm; |
dwianiyulia | 0:cddb02c09d3f | 15 | typedef int IdxType; |
dwianiyulia | 0:cddb02c09d3f | 16 | typedef int ElType; |
dwianiyulia | 0:cddb02c09d3f | 17 | typedef struct{ |
dwianiyulia | 0:cddb02c09d3f | 18 | ElType TI[IdxMax]; |
dwianiyulia | 0:cddb02c09d3f | 19 | int Neff; |
dwianiyulia | 0:cddb02c09d3f | 20 | }TabInt; |
dwianiyulia | 0:cddb02c09d3f | 21 | TabInt A,B,C; |
dwianiyulia | 0:cddb02c09d3f | 22 | IdxType i; |
dwianiyulia | 0:cddb02c09d3f | 23 | const float wheel_radius = 0.050; |
dwianiyulia | 0:cddb02c09d3f | 24 | const float PI = 3.141592; |
dwianiyulia | 0:cddb02c09d3f | 25 | float sample_time = 10; |
dwianiyulia | 0:cddb02c09d3f | 26 | |
dwianiyulia | 0:cddb02c09d3f | 27 | void locomotionMovement(); |
dwianiyulia | 0:cddb02c09d3f | 28 | |
dwianiyulia | 0:cddb02c09d3f | 29 | //Procedures Implementation |
dwianiyulia | 0:cddb02c09d3f | 30 | int main(){ |
dwianiyulia | 0:cddb02c09d3f | 31 | startMillis(); |
dwianiyulia | 0:cddb02c09d3f | 32 | //gerak motor |
dwianiyulia | 0:cddb02c09d3f | 33 | while (1){ |
dwianiyulia | 0:cddb02c09d3f | 34 | if (millis()- last_timer >= sample_time){ |
dwianiyulia | 0:cddb02c09d3f | 35 | //read actual velocity (feedback) |
dwianiyulia | 0:cddb02c09d3f | 36 | velA = (encoderA.getPulses() * 2 * PI * wheel_radius)/(0.003*538); |
dwianiyulia | 0:cddb02c09d3f | 37 | //give pwm to motor |
dwianiyulia | 0:cddb02c09d3f | 38 | while (pwm<=0.8){ |
dwianiyulia | 0:cddb02c09d3f | 39 | pwm=pwm+0.1 |
dwianiyulia | 0:cddb02c09d3f | 40 | } |
dwianiyulia | 0:cddb02c09d3f | 41 | motorA.speed(pwm); |
dwianiyulia | 0:cddb02c09d3f | 42 | //reset encoder |
dwianiyulia | 0:cddb02c09d3f | 43 | encoderA.reset(); |
dwianiyulia | 0:cddb02c09d3f | 44 | last_timer = millis(); |
dwianiyulia | 0:cddb02c09d3f | 45 | pc.printf("%d\n", velA)); |
dwianiyulia | 0:cddb02c09d3f | 46 | } |
dwianiyulia | 0:cddb02c09d3f | 47 | } |
dwianiyulia | 0:cddb02c09d3f | 48 | |
dwianiyulia | 0:cddb02c09d3f | 49 | } |