fugu's CERICA board library. it is use 2019 robocon
Dependencies: ST7032 QEI PS4Serial
Dependents: 2021Arobo_UMAPYOI 2021Arobo_YUMIPYOI
cerica2.cpp
00001 #include "cerica2.h" 00002 00003 CERICA::CERICA(I2C *i2c_,WireSel WireSel_) : 00004 wireSelect(PC_4), 00005 i2c(PB_9,PB_8), 00006 buz(PA_0), 00007 leds(PA_15, PB_7, PC_13, PC_2, PC_3), 00008 sw(PC_0, PC_1, PB_0, PA_4), 00009 dReset(PC_12), // deviceReset pin 00010 xEnc(PC_6, PC_5, PC_8, 360), 00011 yEnc(PA_11, PB_12, PA_12, 360), 00012 brake(false),isEnc(false), 00013 maxSpd(240) 00014 { 00015 dReset = 1; 00016 wireSelect = WireSel_; 00017 ADDR = 0x10 << 1; 00018 sw.mode(OpenDrain); 00019 xEnc.reset(); 00020 yEnc.reset(); 00021 wait_ms(200); 00022 dReset = 0; 00023 } 00024 00025 long CERICA::map(long x, long in_min, long in_max, long out_min, long out_max) 00026 { 00027 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 00028 } 00029 00030 // Motor 00031 bool CERICA::Send(bool available_) 00032 { 00033 bool isI2c = 1; 00034 for (int i = 0; i < 0xF; i++) { 00035 int vInt = abs(power[i]) * available_; 00036 if (vInt > 255) vInt = 255; 00037 unsigned char v = vInt; 00038 char direction; 00039 if (v > 0) { 00040 //if (v > 255) v = 255; 00041 v = map(v, 0, 255, 0, maxSpd); 00042 direction = power[i] < 0; 00043 } else { 00044 direction = (0b10 | !brake); 00045 } 00046 char send[3] = {(char)(0x80 | i), (char)((isEnc << 2) | direction),(char)(v >> 1)}; 00047 00048 wait_us(20); 00049 powerOut[i] = (power[i] < 0) ? v * -1 : v; 00050 isI2c = i2c.write(ADDR, send, 3); 00051 } 00052 return isI2c; 00053 } 00054 00055 bool CERICA::RawMotor(char sendChannel,int verocity) 00056 { 00057 char direction; 00058 if (verocity > 0) { 00059 direction = 0b00; 00060 if (verocity > 255) verocity = 255; 00061 } else if (verocity < 0) { 00062 direction = 0b01; 00063 if (verocity < -255) verocity = -255; 00064 verocity *= -1; 00065 } else direction = 0b10 | !brake; 00066 char send[3] = {(char)(0x80 | sendChannel), (char)((isEnc << 2) | direction), (char)verocity}; 00067 return i2c.write(ADDR, send, 3); 00068 } 00069 00070 00071 // LED 00072 bool CERICA::LED(int n_,bool l_) 00073 { 00074 int mask = ~(1 << n_); 00075 leds = (leds & mask) | (l_ << n_); 00076 return (leds >> n_) & 0x1; 00077 } 00078 00079 // Sound 00080 void CERICA::Play(int scale_) 00081 { 00082 double freq = 440 * pow(2.0,scale_ / 12.0); 00083 buz.period(1 / freq); 00084 buz = 0.5; 00085 } 00086 00087 00088 00089 /*bool CERICA::init(char toMD,bool rvs , bool omni , bool sound , frequency freq , int channel_) 00090 { 00091 char send[3]; 00092 send[0] = 0x90 | channel; 00093 send[1] = omni << 4 | rvs << 2 | sound; 00094 send[2] = freq << 4 | channel_; 00095 channel = channel_; 00096 return i2c.write(ADDR, send, 3); 00097 }*/
Generated on Wed Aug 24 2022 02:04:51 by
1.7.2