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
pwm.cpp@2:4e86f37f1c1c, 2015-10-27 (annotated)
- Committer:
- Blasko
- Date:
- Tue Oct 27 18:58:10 2015 +0000
- Revision:
- 2:4e86f37f1c1c
- Parent:
- 1:0bb74fa30daa
sdf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Blasko | 0:2263e23eb7e8 | 1 | #include "mbed.h" |
Blasko | 0:2263e23eb7e8 | 2 | |
Blasko | 0:2263e23eb7e8 | 3 | Ticker ticker; |
Blasko | 0:2263e23eb7e8 | 4 | DigitalOut led1(LED1); |
Blasko | 0:2263e23eb7e8 | 5 | DigitalOut led2(LED2); |
Blasko | 1:0bb74fa30daa | 6 | PwmOut pwmx(p26); |
Blasko | 1:0bb74fa30daa | 7 | PwmOut pwmy(p25); |
Blasko | 0:2263e23eb7e8 | 8 | CAN can2(p30, p29); |
Blasko | 0:2263e23eb7e8 | 9 | char counter = 0; |
Blasko | 0:2263e23eb7e8 | 10 | int x=0; |
Blasko | 0:2263e23eb7e8 | 11 | int y=0; |
Blasko | 2:4e86f37f1c1c | 12 | int px=0; |
Blasko | 2:4e86f37f1c1c | 13 | int py=0; |
Blasko | 2:4e86f37f1c1c | 14 | int state = 0; // 0 INI 1 JUGANDO 2 ACABADO |
Blasko | 2:4e86f37f1c1c | 15 | |
Blasko | 1:0bb74fa30daa | 16 | |
Blasko | 1:0bb74fa30daa | 17 | #define PERIOD 0.000005 |
Blasko | 1:0bb74fa30daa | 18 | #define MAX 1300.0f |
Blasko | 0:2263e23eb7e8 | 19 | void send() { |
Blasko | 2:4e86f37f1c1c | 20 | //printf("Empezando \n"); |
Blasko | 2:4e86f37f1c1c | 21 | ////printf("send()\n"); |
Blasko | 2:4e86f37f1c1c | 22 | char s[8]; |
Blasko | 2:4e86f37f1c1c | 23 | s[0]=0; |
Blasko | 2:4e86f37f1c1c | 24 | if(can2.write(CANMessage(0, s, 8))) { |
Blasko | 2:4e86f37f1c1c | 25 | // printf("Message sent: \n"); |
Blasko | 2:4e86f37f1c1c | 26 | }else{ |
Blasko | 2:4e86f37f1c1c | 27 | // printf("Message fail: \n"); |
Blasko | 2:4e86f37f1c1c | 28 | } |
Blasko | 0:2263e23eb7e8 | 29 | } |
Blasko | 0:2263e23eb7e8 | 30 | int main() { |
Blasko | 2:4e86f37f1c1c | 31 | printf("Esperando Jugador \n\r"); |
Blasko | 2:4e86f37f1c1c | 32 | // ticker.attach(&send, 1); |
Blasko | 0:2263e23eb7e8 | 33 | CANMessage msg; |
Blasko | 0:2263e23eb7e8 | 34 | can2.frequency(1000000); |
Blasko | 1:0bb74fa30daa | 35 | pwmx.period(PERIOD); // 4 second period |
Blasko | 1:0bb74fa30daa | 36 | pwmy.period(PERIOD); |
Blasko | 0:2263e23eb7e8 | 37 | while(1) { |
Blasko | 0:2263e23eb7e8 | 38 | // printf("loop()\n"); |
Blasko | 2:4e86f37f1c1c | 39 | |
Blasko | 2:4e86f37f1c1c | 40 | if(state == 0){ |
Blasko | 2:4e86f37f1c1c | 41 | if(counter ==100){ led1=!led1; counter =0;} |
Blasko | 2:4e86f37f1c1c | 42 | counter++; |
Blasko | 2:4e86f37f1c1c | 43 | } |
Blasko | 2:4e86f37f1c1c | 44 | |
Blasko | 0:2263e23eb7e8 | 45 | if(can2.read(msg)) { |
Blasko | 2:4e86f37f1c1c | 46 | |
Blasko | 2:4e86f37f1c1c | 47 | if(state == 0){ |
Blasko | 2:4e86f37f1c1c | 48 | // printf("Esperando Jugador"); |
Blasko | 2:4e86f37f1c1c | 49 | if(msg.id == 5){ |
Blasko | 2:4e86f37f1c1c | 50 | printf("Jugando \n\r"); |
Blasko | 2:4e86f37f1c1c | 51 | state=1; |
Blasko | 2:4e86f37f1c1c | 52 | x=0; |
Blasko | 2:4e86f37f1c1c | 53 | y=0; |
Blasko | 2:4e86f37f1c1c | 54 | px = ((int) msg.data[3])*256 + ((int)msg.data[2]); |
Blasko | 2:4e86f37f1c1c | 55 | py = ((int)msg.data[1])*256 + ((int) msg.data[0]); |
Blasko | 2:4e86f37f1c1c | 56 | send(); |
Blasko | 2:4e86f37f1c1c | 57 | // printf("Estado %d %d \n\r",px,py); |
Blasko | 2:4e86f37f1c1c | 58 | } |
Blasko | 2:4e86f37f1c1c | 59 | }else if (state==1) { |
Blasko | 2:4e86f37f1c1c | 60 | if(msg.id == 5){ |
Blasko | 2:4e86f37f1c1c | 61 | send(); |
Blasko | 2:4e86f37f1c1c | 62 | } |
Blasko | 2:4e86f37f1c1c | 63 | if( msg.id ==4 || msg.id ==3){ |
Blasko | 2:4e86f37f1c1c | 64 | x = ((int) msg.data[3])*256 + ((int)msg.data[2]); |
Blasko | 2:4e86f37f1c1c | 65 | y = ((int) msg.data[1])*256 + ((int) msg.data[0]); |
Blasko | 2:4e86f37f1c1c | 66 | pwmx.write(y/MAX); |
Blasko | 2:4e86f37f1c1c | 67 | pwmy.write(1-x/MAX); |
Blasko | 2:4e86f37f1c1c | 68 | |
Blasko | 2:4e86f37f1c1c | 69 | |
Blasko | 2:4e86f37f1c1c | 70 | if(abs(px-x) + abs(py-y) <=50){ |
Blasko | 2:4e86f37f1c1c | 71 | state=2; |
Blasko | 2:4e86f37f1c1c | 72 | if(msg.id==4){ |
Blasko | 2:4e86f37f1c1c | 73 | printf("I lost \n\r"); |
Blasko | 2:4e86f37f1c1c | 74 | }else{ |
Blasko | 2:4e86f37f1c1c | 75 | printf("I won \n\r"); |
Blasko | 2:4e86f37f1c1c | 76 | } |
Blasko | 2:4e86f37f1c1c | 77 | } |
Blasko | 2:4e86f37f1c1c | 78 | } |
Blasko | 2:4e86f37f1c1c | 79 | //printf("Message received: x %d %d %d %d %d %d %d %d %d %d\r\n",msg.data[0],msg.data[1],msg.data[2],msg.data[3],msg.data[4],msg.data[5],msg.data[6],msg.data[7], x , y); |
Blasko | 2:4e86f37f1c1c | 80 | led2 = !led2; |
Blasko | 2:4e86f37f1c1c | 81 | }else{ |
Blasko | 2:4e86f37f1c1c | 82 | wait(5); |
Blasko | 2:4e86f37f1c1c | 83 | state=0; |
Blasko | 2:4e86f37f1c1c | 84 | printf("--------------------------- \n\r"); |
Blasko | 2:4e86f37f1c1c | 85 | printf("Esperando Jugador \n\r"); |
Blasko | 2:4e86f37f1c1c | 86 | } |
Blasko | 2:4e86f37f1c1c | 87 | |
Blasko | 2:4e86f37f1c1c | 88 | // printf("ID: %d \n\r",msg.id); |
Blasko | 2:4e86f37f1c1c | 89 | }else { |
Blasko | 2:4e86f37f1c1c | 90 | |
Blasko | 2:4e86f37f1c1c | 91 | } |
Blasko | 2:4e86f37f1c1c | 92 | // printf("Estado %d x: %d y: %d px: %d py: %d \n\r",state,x,y,px,py); |
Blasko | 2:4e86f37f1c1c | 93 | // wait(0.005); |
Blasko | 0:2263e23eb7e8 | 94 | } |
Blasko | 0:2263e23eb7e8 | 95 | } |