complete motor

Dependencies:   BufferedSerial motor_sn7544

main.cpp

Committer:
kangmingyo
Date:
2019-06-25
Revision:
3:0c6258635590
Parent:
2:a04440f0d001
Child:
4:a72f75611198

File content as of revision 3:0c6258635590:

#include "mbed.h"
#include "motor.h"

MotorCtl motor1(D3,D12,D4,D5);
InterruptIn tachoINT1(D4);
InterruptIn tachoINT2(D5);
Serial pc(USBTX,USBRX,115200);
char rx_buffer[10];
int index=0;
volatile int flag;
void update_current(void)
{
    motor1.UpdateCurrentPosition();
    //  pc.printf("Update Position\r\n");
}
void rx_cb(void){
    char ch;
    ch = pc.getc();
    pc.putc(ch);
    rx_buffer[index++]=ch;
    if(ch==0x0D){
        pc.putc(0x0A);
        rx_buffer[--index]='\0';
        index=0;
        flag=1;
    }
}

void set_speed(void)
{
    int speed;
    speed = atoi((const char*)rx_buffer);
    if(speed>78){speed=78;}
    if(speed<-78){speed=-78;}
    motor1.setTarget(speed);
  //  motor1.setTarget(60);
    pc.printf(" Set speed = %d\r\n", speed);
    
}

int main()
{
    tachoINT1.fall(&update_current);
    tachoINT1.rise(&update_current);
    tachoINT2.fall(&update_current);
    tachoINT2.rise(&update_current);
    pc.attach(callback(rx_cb));
    int rpm;
    float distance;

    while(1) {
        flag=0;
        
        pc.printf("Enter the value for speed [-78,78]\r\n");
        while(flag!=1) {
            rpm=motor1.getRPM();
            distance=motor1.CalculateCumDis();
            printf("distance: %f\r\n",distance);
            wait(1);
        }
//        motor1.setTarget(60);
        set_speed();
    }

}