encslave

Dependencies:   mbed

Committer:
kurobikari
Date:
Wed Sep 01 07:01:56 2021 +0000
Revision:
2:2ee241819861
aaaa

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kurobikari 2:2ee241819861 1 //#include "YKNCT_MD.h"
kurobikari 2:2ee241819861 2 #include "can.h"
kurobikari 2:2ee241819861 3 #include "mbed.h"
kurobikari 2:2ee241819861 4 /*******************************************************************************
kurobikari 2:2ee241819861 5 * @名称 YKNCT_MD_I2C
kurobikari 2:2ee241819861 6 * @概要 I2Cモジュール"MD"用ライブラリ
kurobikari 2:2ee241819861 7 *******************************************************************************/
kurobikari 2:2ee241819861 8
kurobikari 2:2ee241819861 9
kurobikari 2:2ee241819861 10 /* 送信データ
kurobikari 2:2ee241819861 11 [0] [| none | none | none | none | none | none |LAP or SMB| |LAP or SMB|]
kurobikari 2:2ee241819861 12 [1] PWM値1
kurobikari 2:2ee241819861 13 [2] PWM値2
kurobikari 2:2ee241819861 14 */
kurobikari 2:2ee241819861 15
kurobikari 2:2ee241819861 16 YKNCT_MD_CAN::YKNCT_MD_CAN(PinName rd, PinName td,int t) : can(rd, td,t)
kurobikari 2:2ee241819861 17 {
kurobikari 2:2ee241819861 18 /* 初期化 */
kurobikari 2:2ee241819861 19 NumCnt = 0;
kurobikari 2:2ee241819861 20 /* 送信割り込み */
kurobikari 2:2ee241819861 21 interrupt.attach_us(this,&YKNCT_MD_CAN::sent,1800);
kurobikari 2:2ee241819861 22 /* 受信割り込み */
kurobikari 2:2ee241819861 23 can.attach(this,&YKNCT_MD_CAN::onCanReceived);
kurobikari 2:2ee241819861 24 }
kurobikari 2:2ee241819861 25 void YKNCT_MD_CAN::Init(int8_t address, MD_ModeStatus_TypeDef mode)
kurobikari 2:2ee241819861 26 {
kurobikari 2:2ee241819861 27 /* アドレス設定 */
kurobikari 2:2ee241819861 28 txMsg[NumCnt/2].id = address+CAN_TX_ADDRESS;
kurobikari 2:2ee241819861 29 /* モード設定 */
kurobikari 2:2ee241819861 30 txMsg[NumCnt/2].data[0] |= mode<<NumCnt%2;
kurobikari 2:2ee241819861 31 /* 出力状態初期化 */
kurobikari 2:2ee241819861 32 txMsg[NumCnt/2].data[NumCnt%2+1] = 0;
kurobikari 2:2ee241819861 33 /* カウンター増やす */
kurobikari 2:2ee241819861 34 Rx_Id[NumCnt] = CAN_RX_ADDRESS+NumCnt;
kurobikari 2:2ee241819861 35 NumCnt++;
kurobikari 2:2ee241819861 36 }
kurobikari 2:2ee241819861 37 void YKNCT_MD_CAN::Set(int8_t ConNumber, int16_t val)
kurobikari 2:2ee241819861 38 {
kurobikari 2:2ee241819861 39 Set(ConNumber, val, MD_BRAKE);
kurobikari 2:2ee241819861 40 }
kurobikari 2:2ee241819861 41
kurobikari 2:2ee241819861 42 void YKNCT_MD_CAN::Set(int8_t ConNumber, int16_t val, MD_BrakeStatus_TypeDef option)
kurobikari 2:2ee241819861 43 {
kurobikari 2:2ee241819861 44 int PWM = 0;
kurobikari 2:2ee241819861 45 PWM = abs(val);
kurobikari 2:2ee241819861 46 PWM = Rest(PWM,95);
kurobikari 2:2ee241819861 47
kurobikari 2:2ee241819861 48 /* 送るバイト数設定 */
kurobikari 2:2ee241819861 49 txMsg[ConNumber/2].len=3;
kurobikari 2:2ee241819861 50
kurobikari 2:2ee241819861 51 /* idを設定 */
kurobikari 2:2ee241819861 52 txMsg[ConNumber/2].id = ConNumber/2+CAN_TX_ADDRESS;
kurobikari 2:2ee241819861 53
kurobikari 2:2ee241819861 54 /* マイナスならフラグ立てる */
kurobikari 2:2ee241819861 55 if(val<0)
kurobikari 2:2ee241819861 56 txMsg[ConNumber/2].data[ConNumber%2+1] |= (1<<7);
kurobikari 2:2ee241819861 57
kurobikari 2:2ee241819861 58 /* データセット  PWMを各配列に代入 */
kurobikari 2:2ee241819861 59 txMsg[ConNumber/2].data[ConNumber%2+1] |=(char) PWM;
kurobikari 2:2ee241819861 60 }
kurobikari 2:2ee241819861 61
kurobikari 2:2ee241819861 62 int16_t YKNCT_MD_CAN::Get_enc(int8_t ConNumber)
kurobikari 2:2ee241819861 63 {
kurobikari 2:2ee241819861 64
kurobikari 2:2ee241819861 65 return (int)(double)(data[ConNumber]);
kurobikari 2:2ee241819861 66 }
kurobikari 2:2ee241819861 67
kurobikari 2:2ee241819861 68 void YKNCT_MD_CAN::Exe(void)
kurobikari 2:2ee241819861 69 {
kurobikari 2:2ee241819861 70 static int Scnt=0;
kurobikari 2:2ee241819861 71 /* 送信 */
kurobikari 2:2ee241819861 72 can.write(txMsg[Scnt]);
kurobikari 2:2ee241819861 73 Scnt++;
kurobikari 2:2ee241819861 74 Scnt%=NumCnt;
kurobikari 2:2ee241819861 75 }
kurobikari 2:2ee241819861 76
kurobikari 2:2ee241819861 77 void YKNCT_MD_CAN::onCanReceived(void)
kurobikari 2:2ee241819861 78 {
kurobikari 2:2ee241819861 79 static int cnt=0;
kurobikari 2:2ee241819861 80 can.read(rxMsg);
kurobikari 2:2ee241819861 81 for(int i=0; i<NumCnt; i++) {
kurobikari 2:2ee241819861 82 if (rxMsg.id == Rx_Id[i]) {
kurobikari 2:2ee241819861 83 /* エンコーダのデータ受信 一つの基板に付き二つ受信*/
kurobikari 2:2ee241819861 84 data[cnt]=(int)((rxMsg.data[2]<<4|rxMsg.data[1]>>4)&0x0fff);
kurobikari 2:2ee241819861 85 data[cnt+1]=(int)((rxMsg.data[1]<<8|rxMsg.data[0])&0x0fff);
kurobikari 2:2ee241819861 86 }
kurobikari 2:2ee241819861 87 cnt+=2;
kurobikari 2:2ee241819861 88 }
kurobikari 2:2ee241819861 89 cnt=0;
kurobikari 2:2ee241819861 90 }
kurobikari 2:2ee241819861 91
kurobikari 2:2ee241819861 92 void YKNCT_MD_CAN::sent(void)
kurobikari 2:2ee241819861 93 {
kurobikari 2:2ee241819861 94 Exe();
kurobikari 2:2ee241819861 95 }