Raspberry

Dependencies:   mbed Servo TextLCD FastPWM

Committer:
20172573007
Date:
Wed Jul 22 18:42:17 2020 +0000
Revision:
0:c3f67c2fe92d
..

Who changed what in which revision?

UserRevisionLine numberNew contents of line
20172573007 0:c3f67c2fe92d 1 /* mbed Microcontroller Library
20172573007 0:c3f67c2fe92d 2 * Copyright (c) 2018 ARM Limited
20172573007 0:c3f67c2fe92d 3 * SPDX-License-Identifier: Apache-2.0
20172573007 0:c3f67c2fe92d 4 */
20172573007 0:c3f67c2fe92d 5 //librerias
20172573007 0:c3f67c2fe92d 6 #include "mbed.h"
20172573007 0:c3f67c2fe92d 7 #include "Servo.h"
20172573007 0:c3f67c2fe92d 8 #include "TextLCD.h"
20172573007 0:c3f67c2fe92d 9 #include <FastPWM.h>
20172573007 0:c3f67c2fe92d 10
20172573007 0:c3f67c2fe92d 11 //pines
20172573007 0:c3f67c2fe92d 12 Servo myservo(PTE20);
20172573007 0:c3f67c2fe92d 13 PwmOut mypwm(PTA5);
20172573007 0:c3f67c2fe92d 14 Serial pc(USBTX, USBRX);
20172573007 0:c3f67c2fe92d 15 TextLCD lcd(PTB8,PTB9,PTB10,PTB11,PTE2,PTE3,TextLCD::LCD16x2);
20172573007 0:c3f67c2fe92d 16 //algunas variables
20172573007 0:c3f67c2fe92d 17 int l=0;
20172573007 0:c3f67c2fe92d 18 float range = 0.0005;
20172573007 0:c3f67c2fe92d 19 float position = 0.5;
20172573007 0:c3f67c2fe92d 20 int dato6,dato3,dato4,dato5,dato;
20172573007 0:c3f67c2fe92d 21 char dato1,dato2;
20172573007 0:c3f67c2fe92d 22 int flag=0;
20172573007 0:c3f67c2fe92d 23
20172573007 0:c3f67c2fe92d 24 void servo()
20172573007 0:c3f67c2fe92d 25 {
20172573007 0:c3f67c2fe92d 26 switch(pc.getc()) {
20172573007 0:c3f67c2fe92d 27 case 'l':
20172573007 0:c3f67c2fe92d 28 // dato1=pc.getc();//datos char
20172573007 0:c3f67c2fe92d 29 dato2=pc.getc();//datos char
20172573007 0:c3f67c2fe92d 30 dato3=pc.getc();//datos enteros recibidos en codigo ASCII
20172573007 0:c3f67c2fe92d 31 dato4=pc.getc();//datos enteros recibidos en codigo ASCII
20172573007 0:c3f67c2fe92d 32 dato5=pc.getc();//datos enteros recibidos en codigo ASCII
20172573007 0:c3f67c2fe92d 33 dato6=pc.getc();//datos enteros recibidos en codigo ASCII
20172573007 0:c3f67c2fe92d 34 if(dato2=='A'){//numero de dos digitos
20172573007 0:c3f67c2fe92d 35 dato=(dato3-48)*10+dato4-48;
20172573007 0:c3f67c2fe92d 36 }else{//numero de tres digitos(recomendacion enviar @ ).
20172573007 0:c3f67c2fe92d 37 dato=(dato3-48)*100+(dato4-48)*10+dato5-48;
20172573007 0:c3f67c2fe92d 38 }
20172573007 0:c3f67c2fe92d 39 l=dato;
20172573007 0:c3f67c2fe92d 40 break;
20172573007 0:c3f67c2fe92d 41 case 'c':
20172573007 0:c3f67c2fe92d 42 // dato1=pc.getc();//datos char
20172573007 0:c3f67c2fe92d 43 dato2=pc.getc();//datos char
20172573007 0:c3f67c2fe92d 44 dato3=pc.getc();//datos enteros recibidos en codigo ASCII
20172573007 0:c3f67c2fe92d 45 dato4=pc.getc();//datos enteros recibidos en codigo ASCII
20172573007 0:c3f67c2fe92d 46 dato5=pc.getc();//datos enteros recibidos en codigo ASCII
20172573007 0:c3f67c2fe92d 47 dato6=pc.getc();//datos enteros recibidos en codigo ASCII
20172573007 0:c3f67c2fe92d 48 if(dato2=='A'){//numero de dos digitos
20172573007 0:c3f67c2fe92d 49 dato=(dato3-48)*10+dato4-48;
20172573007 0:c3f67c2fe92d 50 }else{//numero de tres digitos(recomendacion enviar @ ).
20172573007 0:c3f67c2fe92d 51 dato=(dato3-48)*100+(dato4-48)*10+dato5-48;
20172573007 0:c3f67c2fe92d 52 }
20172573007 0:c3f67c2fe92d 53 lcd.cls();// borrar antes de mostrar el dato.
20172573007 0:c3f67c2fe92d 54 lcd.locate(0,1); //(columna, fila)
20172573007 0:c3f67c2fe92d 55 lcd.printf("slider %d ",dato);//se imprime el dato de la comunicacion serial.
20172573007 0:c3f67c2fe92d 56
20172573007 0:c3f67c2fe92d 57 break;
20172573007 0:c3f67c2fe92d 58 case 'd':
20172573007 0:c3f67c2fe92d 59 position = 0.0;
20172573007 0:c3f67c2fe92d 60 break;
20172573007 0:c3f67c2fe92d 61 case 'w':
20172573007 0:c3f67c2fe92d 62 position = 0.5;
20172573007 0:c3f67c2fe92d 63 break;
20172573007 0:c3f67c2fe92d 64 case 'a':
20172573007 0:c3f67c2fe92d 65 position = 1.0;
20172573007 0:c3f67c2fe92d 66 break;
20172573007 0:c3f67c2fe92d 67 case 'e':
20172573007 0:c3f67c2fe92d 68 range += 0.0001;
20172573007 0:c3f67c2fe92d 69 break;
20172573007 0:c3f67c2fe92d 70 case 'q':
20172573007 0:c3f67c2fe92d 71 range -= 0.0001;
20172573007 0:c3f67c2fe92d 72 break;
20172573007 0:c3f67c2fe92d 73 }
20172573007 0:c3f67c2fe92d 74 printf("position = %.1f, range = +/-%0.4f\n", position, range);
20172573007 0:c3f67c2fe92d 75 myservo.calibrate(range, 45.0);
20172573007 0:c3f67c2fe92d 76 myservo = position;
20172573007 0:c3f67c2fe92d 77 lcd.locate(3,0);
20172573007 0:c3f67c2fe92d 78
20172573007 0:c3f67c2fe92d 79 }
20172573007 0:c3f67c2fe92d 80
20172573007 0:c3f67c2fe92d 81
20172573007 0:c3f67c2fe92d 82 void led ()
20172573007 0:c3f67c2fe92d 83 {
20172573007 0:c3f67c2fe92d 84
20172573007 0:c3f67c2fe92d 85 mypwm.period_us(5000); //20khz (estandar)
20172573007 0:c3f67c2fe92d 86 mypwm.write(l*0.01);
20172573007 0:c3f67c2fe92d 87 // mypwm.pulsewidth(1);
20172573007 0:c3f67c2fe92d 88 // mypwm.write(i); //duty cycle entre cero y uno
20172573007 0:c3f67c2fe92d 89 // mypwm.write(100/100); //(entrada%*0.01) duty cycle is sense in percentage since 0 to 100
20172573007 0:c3f67c2fe92d 90 }
20172573007 0:c3f67c2fe92d 91 int main()
20172573007 0:c3f67c2fe92d 92 {
20172573007 0:c3f67c2fe92d 93
20172573007 0:c3f67c2fe92d 94 printf("Servo Calibration Controls:\n");
20172573007 0:c3f67c2fe92d 95 printf("1,2,3 - Position Servo (full left, middle, full right)\n");
20172573007 0:c3f67c2fe92d 96 printf("4,5 - Decrease or Increase range\n");
20172573007 0:c3f67c2fe92d 97 while(1) {
20172573007 0:c3f67c2fe92d 98
20172573007 0:c3f67c2fe92d 99 led();
20172573007 0:c3f67c2fe92d 100 servo();
20172573007 0:c3f67c2fe92d 101 }
20172573007 0:c3f67c2fe92d 102 }