Kazyoshi Morita / Mbed 2 deprecated friction_measure

Dependencies:   Dynamixel mbed

main.cpp

Committer:
yamaotoko
Date:
2018-09-07
Revision:
1:f43ebe88228e
Parent:
0:4e684157ecca
Child:
2:d19d83df0d15

File content as of revision 1:f43ebe88228e:

#include "mbed.h"
#include "HX711.hpp"
#include "Dynamixel.h"

HX711 hx711(p21, p22);
Serial pc(USBTX, USBRX);
Dynamixel motor(p13,p14,p15,0xFE,57600);
Timer timeStamp;

//--------------------HX711
void calibrate(float gram){
    hx711.setScale(1.f);
    hx711.tare();
    //この辺をProcessing用に組み替える
    printf("place the weight and put any key\n");
    char c = pc.getc();
    
    float natural = hx711.readGram();
    float calibVal = natural / gram;
    hx711.setScale(calibVal);
    //printf("calibrated! %f %f %d\n", calibVal, natural, hx711.read());
}


//Dynamixel回転方向対応用
//多分11bitのうちMSBが符号,最上位で回転方向を指定している
void move(int speed){
    if(speed >= 0)
        motor.setSpeed(speed);    
    else{
        motor.setSpeed(-1*speed+1024);  //11bitのうちMSBが符号
    }
}

//シリアル受信時
void pc_rx(){
    char c = pc.getc(); //P5からの入力
    
    switch(c){
        case 'c':   //キャリブレーション
            calibrate(50.0);
            break;
        case 's':   //計測開始
            timeStamp.reset();
            move(-20);  //(WIP)
            timeStamp.start();
            break;
        case 'e':   //計測終了
            move(0);    //(WIP)
            timeStamp.stop();
            break;
        case 'z':   //ゼロ点オフセット
            hx711.tare();
            break;
        case 'l':   //モータ左回転
            move(-20);  //(WIP)
            break;
        case 'r':   //モータ右回転
            move(20);   //(WIP)
            break;
        case 'q':   //モータ停止
            move(0);
            break;
        
        case 'm':   //モータ速度設定
            char val_buf[20];   //数値受信用バッファ
            char *p;
            break;
        
        default:
            break;
    }
}

int main() {
    pc.baud(115200);
    //ホイールモードにするための設定
    motor.setAngleLimit(0, ADDRESS_CW_ANGLE_LIMIT);
    motor.setAngleLimit(0, ADDRESS_CCW_ANGLE_LIMIT);
    
    float gram = 0;
    hx711.setScale(1.f);
    hx711.tare();
    
    timeStamp.reset();
    
    while(1) {
        gram = hx711.readGram(3);   //平均回数
        pc.printf(">%f,%f\n", gram, timeStamp.read()); //(WIP)Processingに送るデータ format: >(gram),(Time)
    }
}