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.
Revision 0:cc4ffe9bb764, committed 2018-03-13
- Comitter:
- DANIELOM1916
- Date:
- Tue Mar 13 22:47:00 2018 +0000
- Commit message:
- PICCOLO
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/draw.cpp Tue Mar 13 22:47:00 2018 +0000
@@ -0,0 +1,82 @@
+
+#include "draw.h"
+#include "mbed.h"
+#include "math.h"
+
+
+
+PwmOut myServoX(PB_3); // pin de salida del PWM X
+PwmOut myServoY(PB_4);
+PwmOut myServoZ(PB_5);
+
+uint8_t posx_old=0; // posición anterior del eje X
+uint8_t posy_old=0; // posición anterior del eje Y
+uint8_t ss_time=100; // tiempo de espera para moverse 1 mm en microsegundos
+
+void put_sstime(uint8_t vtime){ //SI
+ ss_time=vtime;
+
+}
+
+int coord2us(float coord)
+{
+ if(0 <= coord <= MAXPOS)
+ return int(560+coord*1700/50);// calculo rango servomotor
+ return 560;
+
+}
+
+void sstime(uint8_t x, uint8_t y) // calculo para la posicion del piccolo
+{
+ double dx=abs(x-posx_old);
+ double dy=abs(y-posy_old);
+ double dist= sqrt(dx*dx+dy*dy);
+ wait_ms((int)(ss_time*dist));
+ posx_old =x;
+ posy_old=y;
+
+ }
+
+void draw(){ // posicion para dibujo
+myServoZ.pulsewidth_us(POSDRAW);
+wait_ms(ss_time*2);
+}
+
+void nodraw(){ // posicion eje z arrina
+myServoZ.pulsewidth_us(MAXPOS);
+wait_ms(ss_time*2);
+}
+
+
+void vertex2d(uint8_t x, uint8_t y){ // subprograma para dibujar
+
+ int pulseX = coord2us(x);
+ int pulseY = coord2us(y);
+
+ myServoX.pulsewidth_us(pulseX);
+ myServoY.pulsewidth_us(pulseY);
+
+ sstime(x,y);
+
+}
+
+void initdraw(float x, float y){ //posicion inicial para dibujar
+ vertex2d (x,y);
+ draw();
+}
+
+void home(){ // posicion en reposo del piccolo
+ nodraw();
+ vertex2d(0 ,0);
+}
+
+
+
+void init_servo() // periodo de lo servos para su funcionamiento
+{
+ myServoX.period_ms(20);
+ myServoY.period_ms(20);
+ myServoZ.period_ms(20);
+
+
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/draw.h Tue Mar 13 22:47:00 2018 +0000 @@ -0,0 +1,20 @@ +#ifndef DRAW_H +#define DRAW_H + +#include "mbed.h" + +#define MAXPOS 50 // en milimetros posicion maxima +#define POSDRAW 30 // en milimetro posicion minima + +// funciones + +void init_servo(); +void draw(); +void nodraw(); +void vertex2d(uint8_t x, uint8_t y); + +void home(); +void initdraw(float x, float y); +void put_sstime(uint8_t vtime); + +#endif // DRAW_H \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Mar 13 22:47:00 2018 +0000
@@ -0,0 +1,152 @@
+#include "mbed.h"
+#include "draw.h"
+
+#define CM_EJECUTAR 0xff // FF COMO EJECUTAR
+#define CM_GUARDAR 0xfe // FE COMO GUARDAR
+#define CM_VERTEX2D 0xfd // FD COMO VERTEX
+#define CM_DRAW 0xfc // FC COMO DIBUJO
+#define CM_NODRAW 0xfb // FB COMO NO DIBUJO
+#define CM_STOP 0xfa // FA COMO PARADA
+#define CM_END 0xf0 // F0 COMO FINALIZAR LINEA
+
+#define MEM_SIZE 10
+#define MEM_TYPE uint32_t
+
+
+int mem_head = 0;
+int mem_tail = 0;
+uint8_t full = 0;
+
+Serial command(USBTX, USBRX); // configurar comando serial
+MEM_TYPE buffer[MEM_SIZE];
+
+
+void ejecutar(){ // subprograma ejecutar
+ command.printf("se esta ejecutando el dibujo...");
+ uint32_t mem_free();
+
+ while (command.getc() != CM_END)
+ {
+ void initdraw(); // posicion inicio de dibujo
+ void memory(); //revisar memoria
+ void dinamic (); //memoria dinamica
+ uint8_t mem_get (); //leer
+
+ switch (command.getc())
+ {
+ case CM_VERTEX2D:
+
+ for (int i=0; i<4; i++){
+ void vertex2d(uint8_t x, uint8_t y);
+ }
+ break;
+ case CM_DRAW:
+ void draw ();
+ break;
+ case CM_NODRAW:
+ void nodraw ();
+ break;
+ default:
+ command.printf("error de comando");
+ break ;
+ }
+ }
+ }
+
+
+void guardar(){ // subprograma guardar
+ command.printf("se inicia el comando de guardar..");
+ uint32_t mem_free();
+ while (command.getc() != CM_END)
+ {
+ void initdraw(); //posicion incial de dibujo
+ void memory();
+ void dinamic ();
+ uint8_t mem_put(); //escribir
+
+ }
+ }
+
+void parada (){ //subprograma de parada
+ command.printf("finalizando programa..");
+ uint32_t mem_free();
+ void nodraw();
+ void home();
+ }
+
+
+void init_serial()
+{
+ command.baud(9600); //configuracion velocidad de transmision
+}
+
+int main() { // programa para reconocer efecutar o guardar
+ init_servo();
+ init_serial();
+ home();
+ char read_cc;
+ while(1)
+ {
+ switch (read_cc) {
+ case CM_EJECUTAR: ejecutar(); break;
+ case CM_GUARDAR: guardar(); break;
+ case CM_STOP: parada (); break;
+ default: command.printf("error de comando");break ;
+ }
+ }
+}
+
+uint32_t mem_free() //memoria libre
+{
+ mem_head=0;
+ full=0;
+}
+
+uint8_t mem_put(MEM_TYPE data) //Escribir en espacio de memoria
+{
+
+ if (full)
+ return 1;
+ buffer[mem_head] = data;
+ mem_head += 1;
+ if (mem_head == MEM_SIZE)
+ full =1;
+ return 0;
+}
+
+uint8_t mem_get(MEM_TYPE* data) // Leer espacio de memoria
+{
+ if (mem_head == 0)
+ return 1;
+ if (mem_head == mem_tail)
+ return 1;
+
+
+ *data = buffer[mem_tail];
+ mem_tail += 1;
+
+ return 0;
+}
+
+
+void dinamic () // memoria dinamica de 32 bits
+ {
+ int temp = command.getc();
+
+ temp = temp<<8 + command.getc();
+ temp = temp<<8 + command.getc();
+ temp = temp<<8 + command.getc();
+ temp = temp<<8 + command.getc();
+ }
+
+void memory () // memoria para obtener el dato
+ {
+ mem_put(command.getc());
+ mem_put(command.getc());
+ mem_put(command.getc());
+ mem_put(command.getc());
+ MEM_TYPE val;
+ mem_get(&val);
+ command.putc(val);
+ }
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Mar 13 22:47:00 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/aa5281ff4a02 \ No newline at end of file