final nnnn
Este proyecto es un Piccolo en el cual se envía comandos para la realización de un dibujo en especifico. El programa va a controlar servos y motores paso a paso teniendo libertad de movimiento en los ejes X,Y,Z; En el programa se utiliza Comunicacion Serial (TX,RX) y para leer el dato fue necesario concatenarlo debido a que recibe 8 Bits pero tenemos que recibir 4 paquetes de 8 Bits para llegar a 32 Bits y poder asi leerlo.
Se estan implementando algunas librerias para su ejecucion como la #include "memory.h" que es de memoria, #include memory_array_h, tambien definimos la memoria #define mm_size 10 de tipo Type Uint32_t, la libreria de operaciones #include "math.h" y la libreria para los motores #include "stepmotor.h".
Para su ejecucion se crearon variables de ejecucion:
CM_DRAWING 0XFF: Se ejecuta siempre y cuando exista datos validos para leer de memoria y nos muestra por medio de un mensaje que se esta ejecutando el dibujo
CM_SAVING 0XFE: Inicia el comando de guardar
CM_VERTEX2D 0XFD: Se encarga de dar las coordenadas a los servomotores en X,Y.
CM_DRAW 0XFC: Se encarga de mover nuestro motor en Z
CM_NODRAW 0XFB: Su funcion es volver a la posicion inicial el motor en el eje Z
CM_MOTOR 0XF9: Se encarga de mover los motores paso a paso para llegar a la ubicacion asignada ingresando el Numero de cuadrantes y su sentido de giro.
draw.cpp@0:0119b611fc51, 2018-04-11 (annotated)
- Committer:
- ANTONIO_VARGAS
- Date:
- Wed Apr 11 02:19:13 2018 +0000
- Revision:
- 0:0119b611fc51
nnhjk
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ANTONIO_VARGAS | 0:0119b611fc51 | 1 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 2 | #include "draw.h" |
ANTONIO_VARGAS | 0:0119b611fc51 | 3 | #include "mbed.h" |
ANTONIO_VARGAS | 0:0119b611fc51 | 4 | #include "math.h" |
ANTONIO_VARGAS | 0:0119b611fc51 | 5 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 6 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 7 | PwmOut myServoZ(PB_3); |
ANTONIO_VARGAS | 0:0119b611fc51 | 8 | PwmOut myServoX(PB_4); |
ANTONIO_VARGAS | 0:0119b611fc51 | 9 | PwmOut myServoY(PB_5); |
ANTONIO_VARGAS | 0:0119b611fc51 | 10 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 11 | uint8_t posx_old=0; // posición anterior del eje X |
ANTONIO_VARGAS | 0:0119b611fc51 | 12 | uint8_t posy_old=0; // posición anterior del eje Y |
ANTONIO_VARGAS | 0:0119b611fc51 | 13 | uint8_t ss_time=50; // tiempo de espera para moverse 1 mm en microsegundos |
ANTONIO_VARGAS | 0:0119b611fc51 | 14 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 15 | void put_sstime(uint8_t vtime){ |
ANTONIO_VARGAS | 0:0119b611fc51 | 16 | ss_time=vtime; |
ANTONIO_VARGAS | 0:0119b611fc51 | 17 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 18 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 19 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 20 | int coord2us(float coord) |
ANTONIO_VARGAS | 0:0119b611fc51 | 21 | { |
ANTONIO_VARGAS | 0:0119b611fc51 | 22 | if(0 <= coord <= MAXPOS) |
ANTONIO_VARGAS | 0:0119b611fc51 | 23 | return int(750+coord*1900/50);// u6 |
ANTONIO_VARGAS | 0:0119b611fc51 | 24 | return 750; |
ANTONIO_VARGAS | 0:0119b611fc51 | 25 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 26 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 27 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 28 | void sstime(uint8_t x, uint8_t y) |
ANTONIO_VARGAS | 0:0119b611fc51 | 29 | { |
ANTONIO_VARGAS | 0:0119b611fc51 | 30 | double dx=abs(x-posx_old); |
ANTONIO_VARGAS | 0:0119b611fc51 | 31 | double dy=abs(y-posy_old); |
ANTONIO_VARGAS | 0:0119b611fc51 | 32 | double dist= sqrt(dx*dx+dy*dy); |
ANTONIO_VARGAS | 0:0119b611fc51 | 33 | wait_ms((int)(ss_time*dist)); |
ANTONIO_VARGAS | 0:0119b611fc51 | 34 | posx_old =x; |
ANTONIO_VARGAS | 0:0119b611fc51 | 35 | posy_old=y; |
ANTONIO_VARGAS | 0:0119b611fc51 | 36 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 37 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 38 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 39 | void draw(){ |
ANTONIO_VARGAS | 0:0119b611fc51 | 40 | myServoZ.pulsewidth_us(coord2us(POSDRAW)); |
ANTONIO_VARGAS | 0:0119b611fc51 | 41 | wait_ms(ss_time*10); |
ANTONIO_VARGAS | 0:0119b611fc51 | 42 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 43 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 44 | void nodraw(){ |
ANTONIO_VARGAS | 0:0119b611fc51 | 45 | myServoZ.pulsewidth_us(coord2us(POSNODRAW)); |
ANTONIO_VARGAS | 0:0119b611fc51 | 46 | wait_ms(ss_time*10); |
ANTONIO_VARGAS | 0:0119b611fc51 | 47 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 48 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 49 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 50 | void vertex2d(uint8_t x, uint8_t y){ |
ANTONIO_VARGAS | 0:0119b611fc51 | 51 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 52 | int pulseX = coord2us(x); |
ANTONIO_VARGAS | 0:0119b611fc51 | 53 | int pulseY = coord2us(y); |
ANTONIO_VARGAS | 0:0119b611fc51 | 54 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 55 | myServoX.pulsewidth_us(pulseX); |
ANTONIO_VARGAS | 0:0119b611fc51 | 56 | myServoY.pulsewidth_us(pulseY); |
ANTONIO_VARGAS | 0:0119b611fc51 | 57 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 58 | sstime(x,y); |
ANTONIO_VARGAS | 0:0119b611fc51 | 59 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 60 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 61 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 62 | void initdraw(float x, float y){ |
ANTONIO_VARGAS | 0:0119b611fc51 | 63 | vertex2d (x,y); |
ANTONIO_VARGAS | 0:0119b611fc51 | 64 | draw(); |
ANTONIO_VARGAS | 0:0119b611fc51 | 65 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 66 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 67 | void home(){ |
ANTONIO_VARGAS | 0:0119b611fc51 | 68 | nodraw(); |
ANTONIO_VARGAS | 0:0119b611fc51 | 69 | vertex2d(0 ,0); |
ANTONIO_VARGAS | 0:0119b611fc51 | 70 | } |
ANTONIO_VARGAS | 0:0119b611fc51 | 71 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 72 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 73 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 74 | void init_servo() |
ANTONIO_VARGAS | 0:0119b611fc51 | 75 | { |
ANTONIO_VARGAS | 0:0119b611fc51 | 76 | myServoX.period_ms(20); |
ANTONIO_VARGAS | 0:0119b611fc51 | 77 | myServoY.period_ms(20); |
ANTONIO_VARGAS | 0:0119b611fc51 | 78 | myServoZ.period_ms(20); |
ANTONIO_VARGAS | 0:0119b611fc51 | 79 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 80 | |
ANTONIO_VARGAS | 0:0119b611fc51 | 81 | } |