ver CAN

Committer:
Wataru Nakata
Date:
Sat Nov 11 09:42:04 2017 +0900
Revision:
12:3fa4f849d6f7
Parent:
9:12ea2a3883bb
Child:
13:ea34af94e90c
added check sum

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 2:75153205d6e8 4 ikarashiMDC::ikarashiMDC(DigitalOut* serialcontrol,uint8_t taddr,uint8_t tmotorNum,bool tmode,Serial *tserial)
WAT34 0:314f2bed3958 5 {
WAT34 2:75153205d6e8 6 serialControl = serialcontrol;
WAT34 1:5e8014a1adbd 7 serialControl->write(true);
WAT34 2:75153205d6e8 8 serial = tserial;
WAT34 2:75153205d6e8 9 addr = taddr;
WAT34 2:75153205d6e8 10 motorNum = tmotorNum;
WAT34 2:75153205d6e8 11 mode = tmode;
WAT34 9:12ea2a3883bb 12 braking = true;
WAT34 0:314f2bed3958 13 }
WAT34 0:314f2bed3958 14
WAT34 2:75153205d6e8 15 int ikarashiMDC::setSpeed(const double& speed)
WAT34 0:314f2bed3958 16 {
Wataru Nakata 12:3fa4f849d6f7 17 uint8_t data[4],dataSpeed;
WAT34 2:75153205d6e8 18 //Limiter
Wataru Nakata 12:3fa4f849d6f7 19 const double cropped_speed = fmin(1.0,fmax(-1.0,speed);
WAT34 2:75153205d6e8 20
WAT34 2:75153205d6e8 21 //dataspeed 0~253 neutaral 126
WAT34 2:75153205d6e8 22 dataSpeed = ((cropped_speed+1.0)/2.0)*253;
WAT34 2:75153205d6e8 23 //printf("%d\n",dataSpeed);
WAT34 2:75153205d6e8 24 //set sending data
WAT34 2:75153205d6e8 25 data[0] = 255; //header
WAT34 7:a8fcb8df927a 26 data[1] = (addr<<5) + motorNum + (mode<<4)+(braking<<3); //address
WAT34 2:75153205d6e8 27 data[2] = dataSpeed;
Wataru Nakata 12:3fa4f849d6f7 28 data[3] = data[1]^data[2];
WAT34 2:75153205d6e8 29 //send data
Wataru Nakata 12:3fa4f849d6f7 30 for(int i=0; i<4; i++) {
WAT34 2:75153205d6e8 31 serial->putc(data[i]);
WAT34 2:75153205d6e8 32 }
WAT34 0:314f2bed3958 33 return 0;
WAT34 0:314f2bed3958 34 }
WAT34 0:314f2bed3958 35
WAT34 2:75153205d6e8 36
WAT34 2:75153205d6e8 37 void estop(Serial *serial)
WAT34 1:5e8014a1adbd 38 {
WAT34 2:75153205d6e8 39 for(int i = 0; i<3; i++) {
WAT34 2:75153205d6e8 40 serial->putc(255);
WAT34 1:5e8014a1adbd 41 }
WAT34 2:75153205d6e8 42
WAT34 5:b43c03ee7f8b 43 }