ver CAN

Committer:
WAT34
Date:
Wed Aug 30 06:55:32 2017 +0000
Revision:
7:a8fcb8df927a
Parent:
2:75153205d6e8
Child:
8:626daa6d81ac
braking added;

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