ver CAN

Committer:
WAT34
Date:
Thu Aug 10 07:17:14 2017 +0000
Revision:
1:5e8014a1adbd
Parent:
0:314f2bed3958
Child:
2:75153205d6e8
worked;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WAT34 0:314f2bed3958 1 #include "ikarashiMDC.h"
WAT34 0:314f2bed3958 2
WAT34 0:314f2bed3958 3
WAT34 0:314f2bed3958 4 ikarashiMDC::ikarashiMDC()
WAT34 0:314f2bed3958 5 {
WAT34 1:5e8014a1adbd 6 serialControl = new DigitalOut(D2);
WAT34 0:314f2bed3958 7 serial=NULL;
WAT34 0:314f2bed3958 8 addr = 0;
WAT34 0:314f2bed3958 9 motorNum = 0;
WAT34 1:5e8014a1adbd 10 serialControl->write(true);
WAT34 0:314f2bed3958 11 }
WAT34 0:314f2bed3958 12
WAT34 0:314f2bed3958 13 int ikarashiMDC::setMotorinfo(uint8_t _addr,uint8_t _motorNum,Serial *_serial)
WAT34 0:314f2bed3958 14 {
WAT34 0:314f2bed3958 15 serial = _serial;
WAT34 0:314f2bed3958 16 addr = _addr;
WAT34 0:314f2bed3958 17 motorNum = _motorNum;
WAT34 0:314f2bed3958 18 return 0;
WAT34 0:314f2bed3958 19 }
WAT34 0:314f2bed3958 20
WAT34 0:314f2bed3958 21 int ikarashiMDC::setMotorSpeed(double speed)
WAT34 0:314f2bed3958 22 {
WAT34 0:314f2bed3958 23 uint8_t data[3],dataSpeed;
WAT34 0:314f2bed3958 24 //Limiter
WAT34 0:314f2bed3958 25 if(speed > 1.0) speed = 1.0;
WAT34 0:314f2bed3958 26 if(speed < -1.0) speed = -1.0;
WAT34 0:314f2bed3958 27
WAT34 0:314f2bed3958 28 //dataspeed 0~253 neutaral 126
WAT34 0:314f2bed3958 29 dataSpeed = ((speed+1.0)/2.0)*253;
WAT34 0:314f2bed3958 30 //set sending data
WAT34 0:314f2bed3958 31 data[0] = 255; //header
WAT34 1:5e8014a1adbd 32 data[1] = addr<<4 + motorNum; //address
WAT34 0:314f2bed3958 33 data[2] = dataSpeed;
WAT34 0:314f2bed3958 34
WAT34 0:314f2bed3958 35 //send data
WAT34 0:314f2bed3958 36 for(int i=0;i<3; i++)
WAT34 0:314f2bed3958 37 {
WAT34 0:314f2bed3958 38 serial->putc(data[i]);
WAT34 0:314f2bed3958 39 }
WAT34 0:314f2bed3958 40 return 0;
WAT34 1:5e8014a1adbd 41 }
WAT34 1:5e8014a1adbd 42 void ikarashiMDC::stop()
WAT34 1:5e8014a1adbd 43 {
WAT34 1:5e8014a1adbd 44 int data[3];
WAT34 1:5e8014a1adbd 45 data[0] = 255;
WAT34 1:5e8014a1adbd 46 data[2] = 126;
WAT34 1:5e8014a1adbd 47 for(int i = 0; i < 16;i++)
WAT34 1:5e8014a1adbd 48 {
WAT34 1:5e8014a1adbd 49 for(int j = 0;j <3;j++)
WAT34 1:5e8014a1adbd 50 {
WAT34 1:5e8014a1adbd 51 for(int k = 0;j<3;j++)
WAT34 1:5e8014a1adbd 52 {
WAT34 1:5e8014a1adbd 53 data[1] = i<<4 + j;
WAT34 1:5e8014a1adbd 54 serial->putc(data[k]);
WAT34 1:5e8014a1adbd 55 }
WAT34 1:5e8014a1adbd 56 }
WAT34 1:5e8014a1adbd 57 }
WAT34 1:5e8014a1adbd 58
WAT34 1:5e8014a1adbd 59 }
WAT34 1:5e8014a1adbd 60 int ikarashiMDC::checkcomm()
WAT34 1:5e8014a1adbd 61 {
WAT34 1:5e8014a1adbd 62 int data[3];
WAT34 1:5e8014a1adbd 63 data[0] =255;
WAT34 1:5e8014a1adbd 64 data[1] = addr<<4 +motorNum;
WAT34 1:5e8014a1adbd 65 data[2]=126;
WAT34 1:5e8014a1adbd 66 for(int i=0;i<3;i++)
WAT34 1:5e8014a1adbd 67 serial->putc(data[i]);
WAT34 1:5e8014a1adbd 68 serialControl->write(false);
WAT34 1:5e8014a1adbd 69 if (serial->readable())
WAT34 1:5e8014a1adbd 70 return serial->getc();
WAT34 1:5e8014a1adbd 71 serialControl->write(true);
WAT34 1:5e8014a1adbd 72 return false;
WAT34 1:5e8014a1adbd 73 }