├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── NecIR.cpp ├── README.md ├── ReceiverIR.cpp ├── ReceiverIR.h ├── RemoteIR.h ├── TransmitterIR.cpp ├── TransmitterIR.h ├── _locales ├── zh-cn │ └── MaqueenPlus-strings.json └── zh-tw │ └── MaqueenPlus-strings.json ├── enums.d.ts ├── icon.png ├── maqueenIR.cpp ├── maqueenplus.ts ├── pin.cpp ├── pxt.json ├── shims.d.ts ├── test.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | built 2 | node_modules 3 | yotta_modules 4 | yotta_targets 5 | pxt_modules 6 | *.db 7 | *.tgz 8 | .header.json 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "8.9.4" 4 | script: 5 | - "npm install -g pxt" 6 | - "pxt target microbit" 7 | - "pxt install" 8 | - "pxt build" 9 | sudo: false 10 | cache: 11 | directories: 12 | - npm_modules 13 | - pxt_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 DFRobot 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: deploy 2 | 3 | build: 4 | pxt build 5 | 6 | deploy: 7 | pxt deploy 8 | 9 | test: 10 | pxt test 11 | -------------------------------------------------------------------------------- /NecIR.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "pxt.h" 3 | 4 | //% color=50 weight=80 5 | //% icon="\uf1eb" 6 | namespace maqueenIRV2 { 7 | int ir_code = 0x00; 8 | int ir_addr = 0x00; 9 | int data; 10 | 11 | int logic_value(){//判断逻辑值"0"和"1"子函数 12 | uint32_t lasttime = system_timer_current_time_us(); 13 | uint32_t nowtime; 14 | while(!uBit.io.P16.getDigitalValue());//低等待 15 | nowtime = system_timer_current_time_us(); 16 | if((nowtime - lasttime) > 400 && (nowtime - lasttime) < 700){//低电平560us 17 | while(uBit.io.P16.getDigitalValue());//是高就等待 18 | lasttime = system_timer_current_time_us(); 19 | if((lasttime - nowtime)>400 && (lasttime - nowtime) < 700){//接着高电平560us 20 | return 0; 21 | }else if((lasttime - nowtime)>1500 && (lasttime - nowtime) < 1800){//接着高电平1.7ms 22 | return 1; 23 | } 24 | } 25 | uBit.serial.printf("error\r\n"); 26 | return -1; 27 | } 28 | 29 | void pulse_deal(){ 30 | int i; 31 | ir_addr=0x00;//清零 32 | for(i=0; i<16;i++ ) 33 | { 34 | if(logic_value() == 1) 35 | { 36 | ir_addr |=(1< 100000){//超过100 ms,表明此时没有按键按下 58 | ir_code = 0xff00; 59 | return; 60 | } 61 | } 62 | //如果高电平持续时间不超过100ms 63 | lasttime = system_timer_current_time_us(); 64 | while(!uBit.io.P16.getDigitalValue());//低等待 65 | nowtime = system_timer_current_time_us(); 66 | if((nowtime - lasttime) < 10000 && (nowtime - lasttime) > 8000){//9ms 67 | while(uBit.io.P16.getDigitalValue());//高等待 68 | lasttime = system_timer_current_time_us(); 69 | if((lasttime - nowtime) > 4000 && (lasttime - nowtime) < 5000){//4.5ms,接收到了红外协议头且是新发送的数据。开始解析逻辑0和1 70 | pulse_deal(); 71 | //uBit.serial.printf("addr=0x%X,code = 0x%X\r\n",ir_addr,ir_code); 72 | data = ir_code; 73 | return;//ir_code; 74 | }else if((lasttime - nowtime) > 2000 && (lasttime - nowtime) < 2500){//2.25ms,表示发的跟上一个包一致 75 | while(!uBit.io.P16.getDigitalValue());//低等待 76 | nowtime = system_timer_current_time_us(); 77 | if((nowtime - lasttime) > 500 && (nowtime - lasttime) < 700){//560us 78 | //uBit.serial.printf("addr=0x%X,code = 0x%X\r\n",ir_addr,ir_code); 79 | data = ir_code; 80 | return;//ir_code; 81 | } 82 | } 83 | } 84 | } 85 | 86 | //% 87 | int irCode(){ 88 | remote_decode(); 89 | return data; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MaqueenPlus 2 | 3 | [Maqueen plus is a STEM educational robot for micro:bit. Optimized with better power management and larger capacity power supply, it can be perfectly compatible with Huskylens AI Vision Sensor.](https://www.dfrobot.com/product-2026.html) 4 | ## Basic usage 5 | 6 | * Control the motor speed and direction; stop motor rotating 7 | 8 | ```blocks 9 | DFRobotMaqueenPlus.I2CInit() 10 | DFRobotMaqueenPlus.mototRun(Motors.ALL, Dir.CW, 255) 11 | DFRobotMaqueenPlus.mototStop(Motors.M1) 12 | ``` 13 | 14 | * Read the speed and direction of the left/right motor 15 | 16 | ```blocks 17 | DFRobotMaqueenPlus.I2CInit() 18 | basic.forever(function () { 19 | serial.writeValue("speed", DFRobotMaqueenPlus.readSpeed(Motors1.M1)) 20 | serial.writeValue("direction", DFRobotMaqueenPlus.readDirection(Motors1.M1)) 21 | }) 22 | ``` 23 | 24 | * Read the state and greyscale value of the line-tracking sensor 25 | 26 | ```blocks 27 | DFRobotMaqueenPlus.I2CInit() 28 | basic.forever(function () { 29 | serial.writeValue("patorl", DFRobotMaqueenPlus.readPatrol(Patrol.L1)) 30 | serial.writeValue("voltage", DFRobotMaqueenPlus.readPatrolVoltage(Patrol.L1)) 31 | }) 32 | 33 | ``` 34 | 35 | * Read IR value and the distance the ultrasound returns 36 | 37 | ```blocks 38 | DFRobotMaqueenPlus.I2CInit() 39 | basic.forever(function () { 40 | serial.writeValue("IR", DFRobotMaqueenPlus.IR_read()) 41 | serial.writeValue("ultrasonic", DFRobotMaqueenPlus.ultraSonic(PIN.P0, PIN.P0)) 42 | }) 43 | 44 | ``` 45 | 46 | * Servo control module for controlling 3-way servo 47 | 48 | ```blocks 49 | DFRobotMaqueenPlus.I2CInit() 50 | DFRobotMaqueenPlus.servoRun(Servos.S1, 100) 51 | 52 | ``` 53 | 54 | * RGB LED control module: let Maqueen Plus's RGB LEDs display various colors 55 | 56 | ```blocks 57 | DFRobotMaqueenPlus.I2CInit() 58 | DFRobotMaqueenPlus.setRGBLight(RGBLight.RGBL, Color.RED) 59 | 60 | ``` 61 | 62 | 63 | ## License 64 | 65 | MIT 66 | 67 | Copyright (c) 2018, microbit/micropython Chinese community 68 | 69 | 70 | ## Supported targets 71 | 72 | * for PXT/microbit 73 | (The metadata above is needed for package search.) 74 | -------------------------------------------------------------------------------- /ReceiverIR.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * IR receiver (Version 0.0.4) 3 | * 4 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) 5 | * http://shinta.main.jp/ 6 | */ 7 | 8 | #include "ReceiverIR.h" 9 | 10 | #define LOCK() 11 | #define UNLOCK() 12 | 13 | #define InRange(x,y) ((((y) * 0.7) < (x)) && ((x) < ((y) * 1.3))) 14 | 15 | /** 16 | * Constructor. 17 | * 18 | * @param rxpin Pin for receive IR signal. 19 | */ 20 | ReceiverIR::ReceiverIR(PinName rxpin) : evt(rxpin) { 21 | init_state(); 22 | evt.fall(this, &ReceiverIR::isr_fall); 23 | evt.rise(this, &ReceiverIR::isr_rise); 24 | evt.mode(PullUp); 25 | ticker.attach_us(this, &ReceiverIR::isr_wdt, 10 * 1000); 26 | } 27 | 28 | /** 29 | * Destructor. 30 | */ 31 | ReceiverIR::~ReceiverIR() { 32 | } 33 | 34 | /** 35 | * Get state. 36 | * 37 | * @return Current state. 38 | */ 39 | ReceiverIR::State ReceiverIR::getState() { 40 | LOCK(); 41 | State s = work.state; 42 | UNLOCK(); 43 | return s; 44 | } 45 | 46 | /** 47 | * Get data. 48 | * 49 | * @param format Pointer to format. 50 | * @param buf Buffer of a data. 51 | * @param bitlength Bit length of the buffer. 52 | * 53 | * @return Data bit length. 54 | */ 55 | int ReceiverIR::getData(RemoteIR::Format *format, uint8_t *buf, int bitlength) { 56 | LOCK(); 57 | if (bitlength < data.bitcount) { 58 | UNLOCK(); 59 | return -1; 60 | } 61 | 62 | const int nbits = data.bitcount; 63 | const int nbytes = data.bitcount / 8 + (((data.bitcount % 8) != 0) ? 1 : 0); 64 | *format = data.format; 65 | for (int i = 0; i < nbytes; i++) { 66 | buf[i] = data.buffer[i]; 67 | } 68 | 69 | init_state(); 70 | 71 | UNLOCK(); 72 | return nbits; 73 | } 74 | 75 | void ReceiverIR::init_state(void) { 76 | work.c1 = -1; 77 | work.c2 = -1; 78 | work.c3 = -1; 79 | work.d1 = -1; 80 | work.d2 = -1; 81 | work.state = Idle; 82 | data.format = RemoteIR::UNKNOWN; 83 | data.bitcount = 0; 84 | timer.stop(); 85 | timer.reset(); 86 | for (int i = 0; i < sizeof(data.buffer); i++) { 87 | data.buffer[i] = 0; 88 | } 89 | } 90 | 91 | void ReceiverIR::isr_wdt(void) { 92 | LOCK(); 93 | static int cnt = 0; 94 | if ((Idle != work.state) || ((0 <= work.c1) || (0 <= work.c2) || (0 <= work.c3) || (0 <= work.d1) || (0 <= work.d2))) { 95 | cnt++; 96 | if (cnt > 50) { 97 | #if 0 98 | printf("# WDT [c1=%d, c2=%d, c3=%d, d1=%d, d2=%d, state=%d, format=%d, bitcount=%d]\n", 99 | work.c1, 100 | work.c2, 101 | work.c3, 102 | work.d1, 103 | work.d2, 104 | work.state, 105 | data.format, 106 | data.bitcount); 107 | #endif 108 | init_state(); 109 | cnt = 0; 110 | } 111 | } else { 112 | cnt = 0; 113 | } 114 | UNLOCK(); 115 | } 116 | 117 | void ReceiverIR::isr_fall(void) { 118 | LOCK(); 119 | switch (work.state) { 120 | case Idle: 121 | if (work.c1 < 0) { 122 | timer.start(); 123 | work.c1 = timer.read_us(); 124 | } else { 125 | work.c3 = timer.read_us(); 126 | int a = work.c2 - work.c1; 127 | int b = work.c3 - work.c2; 128 | if (InRange(a, RemoteIR::TUS_NEC * 16) && InRange(b, RemoteIR::TUS_NEC * 8)) { 129 | /* 130 | * NEC. 131 | */ 132 | data.format = RemoteIR::NEC; 133 | work.state = Receiving; 134 | data.bitcount = 0; 135 | } else if (InRange(a, RemoteIR::TUS_NEC * 16) && InRange(b, RemoteIR::TUS_NEC * 4)) { 136 | /* 137 | * NEC Repeat. 138 | */ 139 | data.format = RemoteIR::NEC_REPEAT; 140 | work.state = Received; 141 | data.bitcount = 0; 142 | work.c1 = -1; 143 | work.c2 = -1; 144 | work.c3 = -1; 145 | work.d1 = -1; 146 | work.d2 = -1; 147 | } else if (InRange(a, RemoteIR::TUS_AEHA * 8) && InRange(b, RemoteIR::TUS_AEHA * 4)) { 148 | /* 149 | * AEHA. 150 | */ 151 | data.format = RemoteIR::AEHA; 152 | work.state = Receiving; 153 | data.bitcount = 0; 154 | } else if (InRange(a, RemoteIR::TUS_AEHA * 8) && InRange(b, RemoteIR::TUS_AEHA * 8)) { 155 | /* 156 | * AEHA Repeat. 157 | */ 158 | data.format = RemoteIR::AEHA_REPEAT; 159 | work.state = Received; 160 | data.bitcount = 0; 161 | work.c1 = -1; 162 | work.c2 = -1; 163 | work.c3 = -1; 164 | work.d1 = -1; 165 | work.d2 = -1; 166 | } else { 167 | init_state(); 168 | } 169 | } 170 | break; 171 | case Receiving: 172 | if (RemoteIR::NEC == data.format) { 173 | work.d2 = timer.read_us(); 174 | int a = work.d2 - work.d1; 175 | if (InRange(a, RemoteIR::TUS_NEC * 3)) { 176 | data.buffer[data.bitcount / 8] |= (1 << (data.bitcount % 8)); 177 | } else if (InRange(a, RemoteIR::TUS_NEC * 1)) { 178 | data.buffer[data.bitcount / 8] &= ~(1 << (data.bitcount % 8)); 179 | } 180 | data.bitcount++; 181 | #if 1 182 | /* 183 | * Length of NEC is always 32 bits. 184 | */ 185 | if (32 <= data.bitcount) { 186 | work.state = Received; 187 | work.c1 = -1; 188 | work.c2 = -1; 189 | work.c3 = -1; 190 | work.d1 = -1; 191 | work.d2 = -1; 192 | } 193 | #else 194 | /* 195 | * Set timeout for tail detection automatically. 196 | */ 197 | timeout.detach(); 198 | timeout.attach_us(this, &ReceiverIR::isr_timeout, RemoteIR::TUS_NEC * 5); 199 | #endif 200 | } else if (RemoteIR::AEHA == data.format) { 201 | work.d2 = timer.read_us(); 202 | int a = work.d2 - work.d1; 203 | if (InRange(a, RemoteIR::TUS_AEHA * 3)) { 204 | data.buffer[data.bitcount / 8] |= (1 << (data.bitcount % 8)); 205 | } else if (InRange(a, RemoteIR::TUS_AEHA * 1)) { 206 | data.buffer[data.bitcount / 8] &= ~(1 << (data.bitcount % 8)); 207 | } 208 | data.bitcount++; 209 | #if 0 210 | /* 211 | * Typical length of AEHA is 48 bits. 212 | * Please check a specification of your remote controller if you find a problem. 213 | */ 214 | if (48 <= data.bitcount) { 215 | data.state = Received; 216 | work.c1 = -1; 217 | work.c2 = -1; 218 | work.c3 = -1; 219 | work.d1 = -1; 220 | work.d2 = -1; 221 | } 222 | #else 223 | /* 224 | * Set timeout for tail detection automatically. 225 | */ 226 | timeout.detach(); 227 | timeout.attach_us(this, &ReceiverIR::isr_timeout, RemoteIR::TUS_AEHA * 5); 228 | #endif 229 | } else if (RemoteIR::SONY == data.format) { 230 | work.d1 = timer.read_us(); 231 | } 232 | break; 233 | case Received: 234 | break; 235 | default: 236 | break; 237 | } 238 | UNLOCK(); 239 | } 240 | 241 | void ReceiverIR::isr_rise(void) { 242 | LOCK(); 243 | switch (work.state) { 244 | case Idle: 245 | if (0 <= work.c1) { 246 | work.c2 = timer.read_us(); 247 | int a = work.c2 - work.c1; 248 | if (InRange(a, RemoteIR::TUS_SONY * 4)) { 249 | data.format = RemoteIR::SONY; 250 | work.state = Receiving; 251 | data.bitcount = 0; 252 | } else { 253 | static const int MINIMUM_LEADER_WIDTH = 150; 254 | if (a < MINIMUM_LEADER_WIDTH) { 255 | init_state(); 256 | } 257 | } 258 | } else { 259 | init_state(); 260 | } 261 | break; 262 | case Receiving: 263 | if (RemoteIR::NEC == data.format) { 264 | work.d1 = timer.read_us(); 265 | } else if (RemoteIR::AEHA == data.format) { 266 | work.d1 = timer.read_us(); 267 | } else if (RemoteIR::SONY == data.format) { 268 | work.d2 = timer.read_us(); 269 | int a = work.d2 - work.d1; 270 | if (InRange(a, RemoteIR::TUS_SONY * 2)) { 271 | data.buffer[data.bitcount / 8] |= (1 << (data.bitcount % 8)); 272 | } else if (InRange(a, RemoteIR::TUS_SONY * 1)) { 273 | data.buffer[data.bitcount / 8] &= ~(1 << (data.bitcount % 8)); 274 | } 275 | data.bitcount++; 276 | #if 1 277 | /* 278 | * How do I know the correct length? (6bits, 12bits, 15bits, 20bits...) 279 | * By a model only? 280 | * Please check a specification of your remote controller if you find a problem. 281 | */ 282 | if (32 <= data.bitcount) { 283 | work.state = Received; 284 | work.c1 = -1; 285 | work.c2 = -1; 286 | work.c3 = -1; 287 | work.d1 = -1; 288 | work.d2 = -1; 289 | } 290 | #else 291 | /* 292 | * Set timeout for tail detection automatically. 293 | */ 294 | timeout.detach(); 295 | timeout.attach_us(this, &ReceiverIR::isr_timeout, RemoteIR::TUS_SONY * 4); 296 | #endif 297 | } 298 | break; 299 | case Received: 300 | break; 301 | default: 302 | break; 303 | } 304 | UNLOCK(); 305 | } 306 | 307 | void ReceiverIR::isr_timeout(void) { 308 | LOCK(); 309 | #if 0 310 | printf("# TIMEOUT [c1=%d, c2=%d, c3=%d, d1=%d, d2=%d, state=%d, format=%d, bitcount=%d]\n", 311 | work.c1, 312 | work.c2, 313 | work.c3, 314 | work.d1, 315 | work.d2, 316 | work.state, 317 | data.format, 318 | data.bitcount); 319 | #endif 320 | if (work.state == Receiving) { 321 | printf("done\r\n"); 322 | work.state = Received; 323 | work.c1 = -1; 324 | work.c2 = -1; 325 | work.c3 = -1; 326 | work.d1 = -1; 327 | work.d2 = -1; 328 | } 329 | UNLOCK(); 330 | } 331 | -------------------------------------------------------------------------------- /ReceiverIR.h: -------------------------------------------------------------------------------- 1 | /** 2 | * IR receiver (Version 0.0.4) 3 | * 4 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) 5 | * http://shinta.main.jp/ 6 | */ 7 | 8 | #ifndef _RECEIVER_IR_H_ 9 | #define _RECEIVER_IR_H_ 10 | 11 | #include 12 | 13 | #include "RemoteIR.h" 14 | 15 | /** 16 | * IR receiver class. 17 | */ 18 | class ReceiverIR { 19 | public: 20 | 21 | /** 22 | * Constructor. 23 | * 24 | * @param rxpin Pin for receive IR signal. 25 | */ 26 | explicit ReceiverIR(PinName rxpin); 27 | 28 | /** 29 | * Destructor. 30 | */ 31 | ~ReceiverIR(); 32 | 33 | /** 34 | * State. 35 | */ 36 | typedef enum { 37 | Idle, 38 | Receiving, 39 | Received 40 | } State; 41 | 42 | /** 43 | * Get state. 44 | * 45 | * @return Current state. 46 | */ 47 | State getState(); 48 | 49 | /** 50 | * Get data. 51 | * 52 | * @param format Pointer to format. 53 | * @param buf Buffer of a data. 54 | * @param bitlength Bit length of the buffer. 55 | * 56 | * @return Data bit length. 57 | */ 58 | int getData(RemoteIR::Format *format, uint8_t *buf, int bitlength); 59 | 60 | private: 61 | 62 | typedef struct { 63 | RemoteIR::Format format; 64 | int bitcount; 65 | uint8_t buffer[64]; 66 | } data_t; 67 | 68 | typedef struct { 69 | State state; 70 | int c1; 71 | int c2; 72 | int c3; 73 | int d1; 74 | int d2; 75 | } work_t; 76 | 77 | InterruptIn evt; /**< Interrupt based input for input. */ 78 | Timer timer; /**< Timer for WDT. */ 79 | Ticker ticker; /**< Tciker for tick. */ 80 | Timeout timeout; /**< Timeout for tail. */ 81 | 82 | data_t data; 83 | work_t work; 84 | 85 | void init_state(void); 86 | 87 | void isr_wdt(void); 88 | void isr_fall(void); 89 | void isr_rise(void); 90 | 91 | /** 92 | * ISR timeout for tail detection. 93 | */ 94 | void isr_timeout(void); 95 | 96 | }; 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /RemoteIR.h: -------------------------------------------------------------------------------- 1 | /** 2 | * IR remote common class (Version 0.0.4) 3 | * 4 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) 5 | * http://shinta.main.jp/ 6 | */ 7 | 8 | #ifndef _REMOTE_IR_H_ 9 | #define _REMOTE_IR_H_ 10 | 11 | class RemoteIR { 12 | public: 13 | 14 | typedef enum { 15 | UNKNOWN, 16 | NEC, 17 | NEC_REPEAT, 18 | AEHA, 19 | AEHA_REPEAT, 20 | SONY 21 | } Format; 22 | 23 | static const int TUS_NEC = 562; 24 | static const int TUS_AEHA = 425; 25 | static const int TUS_SONY = 600; 26 | 27 | private: 28 | RemoteIR(); 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /TransmitterIR.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /TransmitterIR.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /_locales/zh-cn/MaqueenPlus-strings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Motors.M1|block": "左侧", 3 | "Motors.M2|block": "右侧", 4 | "Motors.ALL|block": "全部", 5 | "Motors1.M1|block": "左侧", 6 | "Motors1.M2|block": "右侧", 7 | "PID.OFF|block": "关", 8 | "PID.ON|block": "开", 9 | "Dir.CW|block": "正转", 10 | "Dir.CCW|block": "反转", 11 | "RGBLight.RGBL|block": "左RGB灯", 12 | "RGBLight.RGBR|block": "右RGB灯", 13 | "RGBLight.RGBA|block": "全部RGB灯", 14 | "DFRobotMaqueenPlus.PID|block": "PID开关|%pid", 15 | "DFRobotMaqueenPlus.mototRun|block": "电机|%index方向|%direction速度|%speed", 16 | "DFRobotMaqueenPlus.readSpeed|block": "读电机|%index速度", 17 | "DFRobotMaqueenPlus.readDirection|block": "读电机|%index方向(停止:0,前进:1后退:2)", 18 | "DFRobotMaqueenPlus.servoRun|block": "舵机|%index角度|%angle", 19 | "DFRobotMaqueenPlus.setRGBLight|block": "设置|%rgbshow颜色|%color", 20 | "DFRobotMaqueenPlus.readPatrol|block": "读巡线传感器|%patrol", 21 | "DFRobotMaqueenPlus.readPatrolVoltage|block": "读巡线传感器|%patrol灰度值", 22 | "DFRobotMaqueenPlus.readVersion|block": "获取版本", 23 | "DFRobotMaqueenPlus.ultraSonic|block": "读超声波传感器 TRIG %T ECHO %E 单位:CM", 24 | "DFRobotMaqueenPlus.IR_read|block": "读红外线数值", 25 | "DFRobotMaqueenPlus.IR_callbackUser|block": "当红外接收到", 26 | "DFRobotMaqueenPlus.mototStop|block": "电机|%index停止", 27 | "DFRobotMaqueenPlus.I2CInit|block": "I2C初始化直到成功", 28 | "Color.RED|block": "红色", 29 | "Color.GREEN|block": "绿色", 30 | "Color.BLUE|block": "蓝色", 31 | "Color.YELLOW|block": "黄色", 32 | "Color.PINK|block": "紫色", 33 | "Color.CYAN|block": "青色", 34 | "Color.WHITH|block": "白色", 35 | "Color.OFF|block": "关闭", 36 | "DFRobotMaqueenPlus.clearDistance|block": "清除车轮圈数 %motor", 37 | "DFRobotMaqueenPlus.readeDistance|block": "获取车轮圈数 %motor", 38 | "DFRobotMaqueenPlus.IR_callbackUserV2|block": "当红外接收到", 39 | "DFRobotMaqueenPlus.IR_readV2|block": "读红外值" 40 | } -------------------------------------------------------------------------------- /_locales/zh-tw/MaqueenPlus-strings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Motors.M1|block": "左側", 3 | "Motors.M2|block": "右側", 4 | "Motors.ALL|block": "全部", 5 | "Motors1.M1|block": "左側", 6 | "Motors1.M2|block": "右側", 7 | "PID.OFF|block": "關", 8 | "PID.ON|block": "開", 9 | "Dir.CW|block": "正轉", 10 | "Dir.CCW|block": "反轉", 11 | "RGBLight.RGBL|block": "左RGB燈", 12 | "RGBLight.RGBR|block": "右RGB燈", 13 | "RGBLight.RGBA|block": "全部RGB燈", 14 | "DFRobotMaqueenPlus.PID|block": "PID開關|%pid", 15 | "DFRobotMaqueenPlus.mototRun|block": "馬達 %index 方向 %direction 速度 %speed", 16 | "DFRobotMaqueenPlus.readSpeed|block": "讀取馬達 %index 速度", 17 | "DFRobotMaqueenPlus.readDirection|block": "讀取馬達 %index 方向(停止:0, 前進:1, 後退:2)", 18 | "DFRobotMaqueenPlus.servoRun|block": "伺服機馬達 %index 角度 %angle", 19 | "DFRobotMaqueenPlus.setRGBLight|block": "設置 %rgbshow 顏色 %color", 20 | "DFRobotMaqueenPlus.readPatrol|block": "讀取循跡感測器 %patrol", 21 | "DFRobotMaqueenPlus.readPatrolVoltage|block": "讀取循跡感測器 %patrol 灰度值", 22 | "DFRobotMaqueenPlus.readVersion|block": "獲取版本", 23 | "DFRobotMaqueenPlus.ultraSonic|block": "讀取超音波感測器 TRIG %T ECHO %E 單位:CM", 24 | "DFRobotMaqueenPlus.IR_read|block": "讀取紅外線數值", 25 | "DFRobotMaqueenPlus.IR_callbackUser|block": "當接收到紅外線", 26 | "DFRobotMaqueenPlus.mototStop|block": "馬達 %index停止", 27 | "DFRobotMaqueenPlus.I2CInit|block": "I2C初始化直到成功", 28 | "Color.RED|block": "紅", 29 | "Color.GREEN|block": "綠", 30 | "Color.BLUE|block": "藍", 31 | "Color.YELLOW|block": "黃", 32 | "Color.PINK|block": "紫", 33 | "Color.CYAN|block": "青", 34 | "Color.WHITH|block": "白", 35 | "Color.OFF|block": "關閉", 36 | "DFRobotMaqueenPlus.clearDistance|block": "清除車輪圈數 %motor", 37 | "DFRobotMaqueenPlus.readeDistance|block": "獲取車輪圈數 %motor", 38 | "DFRobotMaqueenPlus.IR_readV2|block": "讀取紅外線數值", 39 | "DFRobotMaqueenPlus.IR_callbackUserV2|block": "當接收到紅外線" 40 | } -------------------------------------------------------------------------------- /enums.d.ts: -------------------------------------------------------------------------------- 1 | // Auto-generated. Do not edit. 2 | 3 | 4 | declare const enum Pins { 5 | P0 = 3, 6 | P1 = 2, 7 | P2 = 1, 8 | P3 = 4, 9 | P4 = 5, 10 | P5 = 17, 11 | P6 = 12, 12 | P7 = 11, 13 | P8 = 18, 14 | P9 = 10, 15 | P10 = 6, 16 | P11 = 26, 17 | P12 = 20, 18 | P13 = 23, 19 | P14 = 22, 20 | P15 = 21, 21 | P16 = 16, 22 | P19 = 0, 23 | P20 = 30, 24 | } 25 | 26 | 27 | declare const enum RemoteButton { 28 | Power = 0x00, 29 | VolUp = 0x01, 30 | FuncStop = 0x02, 31 | LeftTwo = 0x04, 32 | Suspended = 0x05, 33 | RightTwo = 0x06, 34 | Down = 0x08, 35 | VolDown = 0x09, 36 | Up = 0x0a, 37 | Zero = 0x0c, 38 | EQ = 0x0d, 39 | StRept = 0x0e, 40 | One = 0x10, 41 | Two = 0x11, 42 | Three = 0x12, 43 | Four = 0x14, 44 | Five = 0x15, 45 | Six = 0x16, 46 | Seven = 0x18, 47 | Eight = 0x19, 48 | Nine = 0x1a, 49 | } 50 | declare namespace maqueenIR { 51 | } 52 | declare namespace maqueenIRV2 { 53 | } 54 | 55 | // Auto-generated. Do not edit. Really. 56 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DFRobot/pxt-DFRobot-Maqueenplus/8769f31c6b124dfc83d7094c865da379da2aeded/icon.png -------------------------------------------------------------------------------- /maqueenIR.cpp: -------------------------------------------------------------------------------- 1 | #include "pxt.h" 2 | #include 3 | #include 4 | #include "ReceiverIR.h" 5 | using namespace pxt; 6 | typedef vector vA; 7 | 8 | 9 | 10 | enum class Pins{ 11 | P0= 3, 12 | P1= 2, 13 | P2= 1, 14 | P3= 4, 15 | P4= 5, 16 | P5= 17, 17 | P6= 12, 18 | P7= 11, 19 | P8= 18, 20 | P9= 10, 21 | P10= 6, 22 | P11= 26, 23 | P12= 20, 24 | P13= 23, 25 | P14= 22, 26 | P15= 21, 27 | P16= 16, 28 | P19= 0, 29 | P20= 30 30 | }; 31 | 32 | enum class RemoteButton { 33 | Power = 0x00, 34 | VolUp = 0x01, 35 | FuncStop = 0x02, 36 | LeftTwo = 0x04, 37 | Suspended = 0x05, 38 | RightTwo = 0x06, 39 | Down = 0x08, 40 | VolDown = 0x09, 41 | Up = 0x0a, 42 | Zero = 0x0c, 43 | EQ = 0x0d, 44 | StRept = 0x0e, 45 | One = 0x10, 46 | Two = 0x11, 47 | Three = 0x12, 48 | Four = 0x14, 49 | Five = 0x15, 50 | Six = 0x16, 51 | Seven = 0x18, 52 | Eight = 0x19, 53 | Nine = 0x1a 54 | }; 55 | 56 | //% color=50 weight=80 57 | //% icon="\uf1eb" 58 | namespace maqueenIR { 59 | map actions; 60 | map lastact; 61 | Timer tsb; 62 | uint8_t buf[32]; 63 | uint32_t now; 64 | ReceiverIR *rx; 65 | RemoteIR::Format fmt = RemoteIR::UNKNOWN; 66 | int msg; 67 | int IRcallbackNum; 68 | 69 | /** 70 | * button pushed. 71 | */ 72 | //% blockId=ir_received_left_event 73 | //% block="on |%btn| button pressed" 74 | void onPressEvent(RemoteButton btn, Action body) { 75 | //if(actions.find(btn) == actions.end()) actions[btn] = new vector(); 76 | IRcallbackNum=(int)btn; 77 | actions[btn].push_back(body); 78 | } 79 | 80 | void cA(vA runner){for(int i=0;igetData(&fmt, buf, 32 * 8); 84 | /* 85 | uBit.serial.send("."); 86 | uBit.serial.send(buf[0]); 87 | uBit.serial.send("."); 88 | uBit.serial.send(buf[1]); 89 | uBit.serial.send("."); 90 | uBit.serial.send(buf[2]); 91 | uBit.serial.send("."); 92 | uBit.serial.send(buf[3]); 93 | uBit.serial.send("."); 94 | uBit.serial.send(buf[4]); 95 | uBit.serial.send("."); 96 | uBit.serial.send(buf[5]); 97 | uBit.serial.send("."); 98 | uBit.serial.send(buf[6]); 99 | uBit.serial.send("."); 100 | uBit.serial.send(buf[7]); 101 | uBit.serial.send("."); 102 | uBit.serial.send(buf[8]); 103 | uBit.serial.send("."); 104 | uBit.serial.send(buf[9]); 105 | uBit.serial.send("."); 106 | uBit.serial.send(buf[10]); 107 | uBit.serial.send("."); 108 | uBit.serial.send(buf[11]); 109 | uBit.serial.send("."); 110 | uBit.serial.send(buf[12]); 111 | uBit.serial.send("."); 112 | uBit.serial.send(buf[13]); 113 | uBit.serial.send("."); 114 | uBit.serial.send(buf[14]); 115 | uBit.serial.send("."); 116 | uBit.serial.send(buf[15]); 117 | uBit.serial.send("."); 118 | uBit.serial.send(buf[16]); 119 | uBit.serial.send("."); 120 | uBit.serial.send(buf[17]); 121 | uBit.serial.send("."); 122 | uBit.serial.send(buf[18]); 123 | uBit.serial.send("."); 124 | uBit.serial.send(buf[19]); 125 | uBit.serial.send("."); 126 | uBit.serial.send(buf[20]); 127 | uBit.serial.send("."); 128 | uBit.serial.send(buf[21]); 129 | uBit.serial.send("."); 130 | uBit.serial.send(buf[22]); 131 | uBit.serial.send("."); 132 | uBit.serial.send(buf[23]); 133 | uBit.serial.send("."); 134 | uBit.serial.send(buf[24]); 135 | uBit.serial.send("."); 136 | uBit.serial.send(buf[25]); 137 | uBit.serial.send("."); 138 | uBit.serial.send(buf[26]); 139 | uBit.serial.send("."); 140 | uBit.serial.send(buf[27]); 141 | uBit.serial.send("."); 142 | uBit.serial.send(buf[28]); 143 | uBit.serial.send("."); 144 | uBit.serial.send(buf[29]); 145 | uBit.serial.send("."); 146 | uBit.serial.send(buf[30]); 147 | uBit.serial.send("."); 148 | uBit.serial.send(buf[31]); 149 | uBit.serial.send("."); 150 | uBit.serial.send(buf[32]); 151 | uBit.serial.send("."); 152 | uBit.serial.send(buf[33]); 153 | uBit.serial.send("."); 154 | uBit.serial.send(buf[34]); 155 | uBit.serial.send("."); 156 | uBit.serial.send(buf[35]); 157 | uBit.serial.send(".end"); 158 | */ 159 | //if(actions.find((RemoteButton)buf[2]) == actions.end()) return; 160 | now = tsb.read_ms(); 161 | if(now - lastact[(RemoteButton)buf[2]] < 100) return; 162 | lastact[(RemoteButton)buf[2]] = now; 163 | msg=(int)buf[2]; 164 | //uBit.serial.send(IRcallbackNum); 165 | if(IRcallbackNum < 1){ 166 | return; 167 | } 168 | for(int i=1;i<=IRcallbackNum;i++){ 169 | cA(actions[(RemoteButton)i]); 170 | } 171 | } 172 | 173 | void monitorIR(){ 174 | while(1){ 175 | while(rx->getState() != ReceiverIR::Received){ 176 | uBit.sleep(50); 177 | } 178 | onReceivable(); 179 | } 180 | } 181 | 182 | /** 183 | * initialises local variablesssss 184 | */ 185 | //% blockId=ir_init 186 | //% block="connect ir receiver to %pin" 187 | void initIR(Pins pin){ 188 | rx = new ReceiverIR((PinName)pin); 189 | tsb.start(); //interrupt timer for debounce 190 | create_fiber(monitorIR); 191 | } 192 | //% 193 | int getParam(){ 194 | return msg; 195 | } 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | } -------------------------------------------------------------------------------- /maqueenplus.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @file pxt-DFRobot_Maqueenplus/maqueenplus.ts 3 | * @brief DFRobot's maqueenplus makecode library. 4 | * @n [Get the module here](https://www.dfrobot.com/product-2026.html) 5 | * @n Maqueen plus is a STEM educational robot for micro:bit. Optimized with better power management and larger capacity power supply, it can be perfectly compatible with Huskylens AI Vision Sensor. 6 | * 7 | * @copyright [DFRobot](http://www.dfrobot.com), 2016 8 | * @copyright MIT Lesser General Public License 9 | * 10 | * @author [email](jie.tang@dfrobot.com) 11 | * @date 2019-11-19 12 | */ 13 | 14 | enum PIN { 15 | P0 = 3, 16 | P1 = 2, 17 | P2 = 1, 18 | P8 = 18, 19 | //P9 = 10, 20 | P12 = 20, 21 | P13 = 23, 22 | P14 = 22, 23 | P15 = 21, 24 | }; 25 | 26 | enum Motors { 27 | //% block="left" 28 | M1 = 1, 29 | //% block="right" 30 | M2 = 2, 31 | //% block="ALL" 32 | ALL = 3 33 | } 34 | 35 | enum Motors1 { 36 | //% block="left" 37 | M1 = 1, 38 | //% block="right" 39 | M2 = 2, 40 | } 41 | 42 | enum Dir { 43 | //% block="rotate forward" 44 | CW = 1, 45 | //% block="backward" 46 | CCW = 2 47 | } 48 | 49 | enum Servos { 50 | //% block="S1" 51 | S1 = 1, 52 | //% block="S2" 53 | S2 = 2, 54 | //% block="S3" 55 | S3 = 3 56 | } 57 | 58 | enum RGBLight { 59 | //%block="RGB_L" 60 | RGBL = 1, 61 | //%block="RGB_R" 62 | RGBR = 2, 63 | //%block="ALL" 64 | RGBA = 3 65 | } 66 | 67 | enum Patrol { 68 | //% block="L1" 69 | L1 = 1, 70 | //%block="L2" 71 | L2 = 2, 72 | //%block="L3" 73 | L3 = 5, 74 | //%block="R1" 75 | R1 = 3, 76 | //%block="R2" 77 | R2 = 4, 78 | //%block="R3" 79 | R3 = 6 80 | } 81 | 82 | enum Sonicunit { 83 | //% block="cm" 84 | Centimeters 85 | } 86 | 87 | enum PID { 88 | //%block="OFF" 89 | OFF = 0, 90 | //%block="ON" 91 | ON = 1 92 | } 93 | 94 | enum Color { 95 | //%block="Red" 96 | RED = 1, 97 | //%block="Green" 98 | GREEN = 2, 99 | //%block="Blue" 100 | BLUE = 4, 101 | //%block="Yellow" 102 | YELLOW = 3, 103 | //%block="Violet" 104 | PINK = 5, 105 | //%block="Cyan" 106 | CYAN = 6, 107 | //%block="White" 108 | WHITH = 7, 109 | //%block="OFF" 110 | OFF = 8 111 | 112 | } 113 | 114 | //% weight=100 color=#00A654 block="Maqueen Plus" icon="\uf067" 115 | namespace DFRobotMaqueenPlus { 116 | let irstate:number; 117 | let state:number; 118 | export class Packeta { 119 | public mye: string; 120 | public myparam: number; 121 | } 122 | 123 | /** 124 | * Init I2C until success 125 | */ 126 | //% weight=100 127 | //%block="initialize via I2C until success" 128 | export function I2CInit():void{ 129 | let Version_v = 0; 130 | pins.i2cWriteNumber(0x10, 0x32, NumberFormat.Int8LE); 131 | Version_v = pins.i2cReadNumber(0x10, NumberFormat.Int8LE); 132 | while (Version_v==0){ 133 | basic.showLeds(` 134 | # . . . # 135 | . # . # . 136 | . . # . . 137 | . # . # . 138 | # . . . # 139 | `, 10) 140 | basic.pause(500) 141 | basic.clearScreen() 142 | pins.i2cWriteNumber(0x10, 0x32, NumberFormat.Int8LE); 143 | Version_v = pins.i2cReadNumber(0x10, NumberFormat.Int8LE); 144 | } 145 | basic.showLeds(` 146 | . . . . . 147 | . . . . # 148 | . . . # . 149 | # . # . . 150 | . # . . . 151 | `, 10) 152 | basic.pause(500) 153 | basic.clearScreen() 154 | } 155 | 156 | /** 157 | * PID control module 158 | */ 159 | //% weight=90 160 | //%block="PID switch|%pid" 161 | export function PID(pid: PID): void { 162 | let buf = pins.createBuffer(2); 163 | buf[0] = 0x0A; 164 | buf[1] = pid; 165 | pins.i2cWriteBuffer(0x10, buf); 166 | } 167 | /** 168 | * Motor control module 169 | */ 170 | //% weight=80 171 | //% block="motor|%index|direction|%direction|speed|%speed " 172 | //% speed.min=0 speed.max=255 173 | export function mototRun(index: Motors, direction: Dir, speed: number): void { 174 | let _speed:number; 175 | if(speed >= 240) _speed=240; 176 | else _speed=speed; 177 | if (index == 1) { 178 | let buf = pins.createBuffer(3) 179 | buf[0] = 0x00; 180 | buf[1] = direction; 181 | buf[2] = _speed; 182 | pins.i2cWriteBuffer(0x10, buf) 183 | 184 | } if (index == 2) { 185 | let buf = pins.createBuffer(3) 186 | buf[0] = 0x02; 187 | buf[1] = direction; 188 | buf[2] = _speed; 189 | pins.i2cWriteBuffer(0x10, buf) 190 | } 191 | if (index == 3) { 192 | let buf = pins.createBuffer(5) 193 | buf[0] = 0x00; 194 | buf[1] = direction; 195 | buf[2] = _speed; 196 | buf[3] = direction; 197 | buf[4] = _speed; 198 | pins.i2cWriteBuffer(0x10, buf) 199 | } 200 | } 201 | /** 202 | * Motor stop module 203 | */ 204 | //% weight=75 205 | //% block="Motor|%index stop" 206 | export function mototStop(index: Motors): void { 207 | 208 | if (index == 1) { 209 | let buf = pins.createBuffer(3) 210 | buf[0] = 0x00; 211 | buf[1] = 0; 212 | buf[2] = 0; 213 | pins.i2cWriteBuffer(0x10, buf) 214 | 215 | } if (index == 2) { 216 | let buf = pins.createBuffer(3) 217 | buf[0] = 0x02; 218 | buf[1] = 0; 219 | buf[2] = 0; 220 | pins.i2cWriteBuffer(0x10, buf) 221 | } 222 | if (index == 3) { 223 | let buf = pins.createBuffer(5) 224 | buf[0] = 0x00; 225 | buf[1] = 0; 226 | buf[2] = 0; 227 | buf[3] = 0; 228 | buf[4] = 0; 229 | pins.i2cWriteBuffer(0x10, buf) 230 | } 231 | } 232 | 233 | 234 | /** 235 | * Compensate speed difference between two motors 236 | */ 237 | // //% weight=7 238 | // //% block="motor compensation|%motor speed|%speed" 239 | // //% speed.min=0 speed.max=255 240 | // export function mostotCompensation(motor: Motors1, speed: number): void { 241 | // let buf = pins.createBuffer(2) 242 | // if (motor == 1) { 243 | // buf[0] = 0x08; 244 | // buf[1] = speed; 245 | // pins.i2cWriteBuffer(0x10, buf) 246 | // } else if (motor == 2) { 247 | // buf[0] = 0x09; 248 | // buf[1] = speed; 249 | // pins.i2cWriteBuffer(0x10, buf) 250 | // } 251 | // } 252 | 253 | /** 254 | * Read motor speed 255 | */ 256 | //% weight=65 257 | //%block="read motor|%index speed" 258 | export function readSpeed(index: Motors1): number { 259 | let _speed:number,ret = -1; 260 | pins.i2cWriteNumber(0x10, 0, NumberFormat.Int8LE) 261 | let speed_x = pins.i2cReadBuffer(0x10, 4) 262 | if (index == 1) { 263 | if((Math.round(speed_x[1])<20) && (Math.round(speed_x[1]) != 0)){ 264 | ret = Math.round(speed_x[1]) + 255; 265 | }else{ 266 | ret = Math.round(speed_x[1]); 267 | } 268 | } else if (index == 2) { 269 | if((Math.round(speed_x[3])<20) && (Math.round(speed_x[3]) != 0)){ 270 | ret = Math.round(speed_x[3]) + 255; 271 | }else{ 272 | ret = Math.round(speed_x[3]); 273 | } 274 | } 275 | return ret; 276 | } 277 | /** 278 | * Read motor direction(stop:0,forward:1,back:2) 279 | */ 280 | //% weight=61 281 | //%block="read motor|%index direction(stop:0,forward:1,back:2)" 282 | export function readDirection(index: Motors1): number { 283 | pins.i2cWriteNumber(0x10, 0, NumberFormat.Int8LE) 284 | let dir_x = pins.i2cReadBuffer(0x10, 4) 285 | if (index == 1) { 286 | return dir_x[0] 287 | 288 | } else if (index == 2) { 289 | return dir_x[2] 290 | } 291 | return -1 292 | } 293 | 294 | /** 295 | * Servo control module 296 | */ 297 | //% weight=40 298 | //% block="servo|%index|angle|%angle" 299 | //% angle.min=0 angle.max=180 300 | export function servoRun(index: Servos, angle: number): void { 301 | let buf = pins.createBuffer(2) 302 | switch (index) { 303 | case 1: 304 | buf[0] = 0x14; 305 | buf[1] = angle; 306 | pins.i2cWriteBuffer(0x10, buf); 307 | break; 308 | case 2: 309 | buf[0] = 0x15; 310 | buf[1] = angle; 311 | pins.i2cWriteBuffer(0x10, buf); 312 | break; 313 | default: 314 | buf[0] = 0x16; 315 | buf[1] = angle; 316 | pins.i2cWriteBuffer(0x10, buf); 317 | break; 318 | } 319 | } 320 | 321 | /** 322 | * Control the color of RGB LED 323 | */ 324 | //% weight=50 325 | //% block="set |%rgbshow color|%color" 326 | export function setRGBLight(rgbshow: RGBLight, color: Color): void { 327 | 328 | if (rgbshow == 1) { 329 | let buf = pins.createBuffer(2) 330 | buf[0] = 0x0B; 331 | buf[1] = color; 332 | pins.i2cWriteBuffer(0x10, buf); 333 | } if (rgbshow == 2) { 334 | let buf = pins.createBuffer(2) 335 | buf[0] = 0x0C; 336 | buf[1] = color; 337 | pins.i2cWriteBuffer(0x10, buf); 338 | } if (rgbshow == 3) { 339 | let buf = pins.createBuffer(3) 340 | buf[0] = 0x0B; 341 | buf[1] = color; 342 | buf[2] = color; 343 | pins.i2cWriteBuffer(0x10, buf); 344 | } 345 | 346 | } 347 | 348 | /** 349 | * Read line-tracking sensor status 350 | */ 351 | //% weight=56 352 | //%block="read line-tracking sensor|%patrol" 353 | export function readPatrol(patrol: Patrol): number { 354 | pins.i2cWriteNumber(0x10, 0x1D, NumberFormat.Int8LE); 355 | let patrol_y = pins.i2cReadBuffer(0x10, 1); 356 | let mark: number ; 357 | switch (patrol) { 358 | case 1: mark = (patrol_y[0] & 0x04) == 0x04 ? 1 : 0; break; 359 | case 2: mark = (patrol_y[0] & 0x02) == 0x02 ? 1 : 0; break; 360 | case 3: mark = (patrol_y[0] & 0x08) == 0x08 ? 1 : 0; break; 361 | case 4: mark = (patrol_y[0] & 0x10) == 0x10 ? 1 : 0; break; 362 | case 5: mark = (patrol_y[0] & 0x01) == 0x01 ? 1 : 0; break; 363 | case 6: mark = (patrol_y[0] & 0x20) == 0x20 ? 1 : 0; break; 364 | } 365 | 366 | return mark 367 | } 368 | 369 | /** 370 | * Read grayscale value of line-tracking sensor 371 | */ 372 | //% weight=55 373 | //% block="read line-tracking sensor|%patrol grayscale " 374 | export function readPatrolVoltage(patrol: Patrol): number { 375 | pins.i2cWriteNumber(0x10, 0x1E, NumberFormat.Int8LE); 376 | let patrolv_y = pins.i2cReadBuffer(0x10, 12); 377 | let patrol_AD: number; 378 | switch (patrol) { 379 | case 1: 380 | patrol_AD = patrolv_y[5] | patrolv_y[4] << 8; 381 | break; 382 | case 2: 383 | patrol_AD = patrolv_y[3] | patrolv_y[2] << 8; 384 | break; 385 | case 3: 386 | patrol_AD = patrolv_y[7] | patrolv_y[6] << 8; 387 | break; 388 | case 4: 389 | patrol_AD = patrolv_y[9] | patrolv_y[8] << 8; 390 | break; 391 | case 5: 392 | patrol_AD = patrolv_y[1] | patrolv_y[0] << 8; 393 | break; 394 | default: 395 | patrol_AD = patrolv_y[11] | patrolv_y[10] << 8; 396 | break; 397 | 398 | } 399 | return patrol_AD; 400 | } 401 | /** 402 | * Get product information 403 | */ 404 | //% weight=5 405 | //%block="get product information" 406 | export function readVersion(): string { 407 | pins.i2cWriteNumber(0x10, 0x32, NumberFormat.Int8LE); 408 | let Version_v = pins.i2cReadNumber(0x10, NumberFormat.Int8LE); 409 | pins.i2cWriteNumber(0x10, 0x33, NumberFormat.Int8LE); 410 | let Version_y = pins.i2cReadBuffer(0x10, Version_v); 411 | let Version_x = Version_y.toString(); 412 | return Version_x; 413 | } 414 | /** 415 | * Read the distance value the ultrasound returns 416 | */ 417 | let state1 = 0; 418 | //% weight=20 419 | //%block="read ultrasonic sensor TRIG %T ECHO %E Company:CM" 420 | export function ultraSonic(T: PIN, E: PIN): number { 421 | 422 | let data; 423 | let i = 0; 424 | data = readUlt(T, E); 425 | if(state1 == 1 && data != 0){ 426 | state1 =0; 427 | } 428 | if(data != 0){ 429 | }else{ 430 | if(state1 == 0){ 431 | do{ 432 | data = readUlt(T, E); 433 | i++; 434 | if(i > 3){ 435 | state1 =1; 436 | data =500; 437 | break; 438 | } 439 | }while(data == 0) 440 | } 441 | } 442 | if(data == 0) 443 | data = 500 444 | return data; 445 | } 446 | 447 | function readUlt(T:number, E:number):number{ 448 | let maxCmDistance = 500; 449 | let _T; 450 | let _E; 451 | switch (T) { 452 | case PIN.P0: _T = DigitalPin.P0; break; 453 | case PIN.P1: _T = DigitalPin.P1; break; 454 | case PIN.P2: _T = DigitalPin.P2; break; 455 | case PIN.P8: _T = DigitalPin.P8; break; 456 | case PIN.P12: _T = DigitalPin.P12; break; 457 | // case PIN.P10: _T = DigitalPin.P10; break; 458 | case PIN.P13: _T = DigitalPin.P13; break; 459 | case PIN.P14: _T = DigitalPin.P14; break; 460 | case PIN.P15: _T = DigitalPin.P15; break; 461 | default: _T = DigitalPin.P0; break; 462 | } 463 | 464 | switch (E) { 465 | case PIN.P0: _E = DigitalPin.P0; break; 466 | case PIN.P1: _E = DigitalPin.P1; break; 467 | case PIN.P2: _E = DigitalPin.P2; break; 468 | case PIN.P8: _E = DigitalPin.P8; break; 469 | //case PIN.P9: _E = DigitalPin.P9; break; 470 | case PIN.P12: _E = DigitalPin.P12; break; 471 | case PIN.P13: _E = DigitalPin.P13; break; 472 | case PIN.P14: _E = DigitalPin.P14; break; 473 | case PIN.P15: _E = DigitalPin.P15; break; 474 | default: _E = DigitalPin.P0; break; 475 | } 476 | 477 | let ultraSonic_d; 478 | // pins.digitalWritePin(_T, 1); 479 | // basic.pause(1) 480 | pins.digitalWritePin(_T, 0); 481 | if (pins.digitalReadPin(_E) == 0) { 482 | pins.digitalWritePin(_T, 0); 483 | pins.digitalWritePin(_T, 1); 484 | basic.pause(20); 485 | pins.digitalWritePin(_T, 0); 486 | ultraSonic_d = pins.pulseIn(_E, PulseValue.High, maxCmDistance * 58); 487 | } else { 488 | pins.digitalWritePin(_T, 1); 489 | pins.digitalWritePin(_T, 0); 490 | basic.pause(20); 491 | pins.digitalWritePin(_T, 0); 492 | ultraSonic_d = pins.pulseIn(_E, PulseValue.Low, maxCmDistance * 58); 493 | } 494 | let ultraSonic_x = ultraSonic_d / 59; 495 | // if (ultraSonic_x <= 0 || ultraSonic_x > 300) { 496 | // return 0; 497 | // } 498 | return Math.round(ultraSonic_x); 499 | } 500 | 501 | /** 502 | * get the revolutions of wheel 503 | */ 504 | //% weight=60 505 | //%block="get the revolutions of wheel %motor" 506 | export function readeDistance(motor:Motors1):string { 507 | let distance:number; 508 | pins.i2cWriteNumber(0x10, 4, NumberFormat.Int8LE) 509 | let speed_x = pins.i2cReadBuffer(0x10, 4) 510 | switch(motor){ 511 | case 1:distance = ((speed_x[0]<<8|speed_x[1])*10)/900;break; 512 | default:distance = ((speed_x[2]<<8|speed_x[3])*10)/900;break; 513 | } 514 | let index=distance.toString().indexOf("."); 515 | let x:string=distance.toString().substr(0,index+3) 516 | return x; 517 | basic.pause(30) 518 | } 519 | /** 520 | * clear the revolutions of wheel 521 | */ 522 | //% weight=60 523 | //%block="clear the revolutions of wheel %motor" 524 | export function clearDistance(motor:Motors):void{ 525 | 526 | switch(motor){ 527 | case 1: 528 | let buf1 = pins.createBuffer(2); 529 | buf1[0] = 0x04; 530 | buf1[1] = 0; 531 | pins.i2cWriteBuffer(0x10, buf1); 532 | break; 533 | case 2: 534 | let buf2 = pins.createBuffer(2); 535 | buf2[0] = 0x06; 536 | buf2[1] = 0; 537 | pins.i2cWriteBuffer(0x10, buf2); 538 | break; 539 | default: 540 | let buf3 = pins.createBuffer(4); 541 | buf3[0] = 0x04; 542 | buf3[1] = 0; 543 | buf3[2] = 0; 544 | buf3[3] = 0; 545 | pins.i2cWriteBuffer(0x10, buf3); 546 | } 547 | } 548 | } 549 | -------------------------------------------------------------------------------- /pin.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DFRobot/pxt-DFRobot-Maqueenplus/8769f31c6b124dfc83d7094c865da379da2aeded/pin.cpp -------------------------------------------------------------------------------- /pxt.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "MaqueenPlus", 3 | "version": "1.5.1", 4 | "description": "", 5 | "dependencies": { 6 | "core": "*", 7 | "ir": "github:tangjie133/pxt-DFRobot_IR#v0.2.0" 8 | }, 9 | "files": [ 10 | "README.md", 11 | "maqueenplus.ts", 12 | "_locales/zh-cn/MaqueenPlus-strings.json", 13 | "_locales/zh-tw/MaqueenPlus-strings.json" 14 | ], 15 | "testFiles": [ 16 | "test.ts" 17 | ], 18 | "targetVersions": { 19 | "target": "5.0.12", 20 | "targetId": "microbit" 21 | }, 22 | "supportedTargets": [ 23 | "microbit" 24 | ], 25 | "preferredEditor": "tsprj" 26 | } 27 | -------------------------------------------------------------------------------- /shims.d.ts: -------------------------------------------------------------------------------- 1 | // Auto-generated. Do not edit. 2 | 3 | 4 | 5 | //% color=50 weight=80 6 | //% icon="\uf1eb" 7 | declare namespace maqueenIR { 8 | 9 | /** 10 | * button pushed. 11 | */ 12 | //% blockId=ir_received_left_event 13 | //% block="on |%btn| button pressed" shim=maqueenIR::onPressEvent 14 | function onPressEvent(btn: RemoteButton, body: () => void): void; 15 | 16 | /** 17 | * initialises local variablesssss 18 | */ 19 | //% blockId=ir_init 20 | //% block="connect ir receiver to %pin" shim=maqueenIR::initIR 21 | function initIR(pin: Pins): void; 22 | } 23 | 24 | 25 | 26 | //% color=50 weight=80 27 | //% icon="\uf1eb" 28 | declare namespace maqueenIRV2 { 29 | } 30 | 31 | // Auto-generated. Do not edit. Really. 32 | -------------------------------------------------------------------------------- /test.ts: -------------------------------------------------------------------------------- 1 | 2 | DFRobotMaqueenPlus.I2CInit() 3 | DFRobotMaqueenPlus.mototRun(Motors.ALL, Dir.CW, 100) 4 | basic.pause(1000) 5 | DFRobotMaqueenPlus.mototStop(Motors.ALL) 6 | basic.pause(1000) 7 | DFRobotMaqueenPlus.setRGBLight(RGBLight.RGBL, Color.RED) 8 | basic.pause(1000) 9 | DFRobotMaqueenPlus.setRGBLight(RGBLight.RGBL, Color.YELLOW) 10 | basic.pause(1000) 11 | DFRobotMaqueenPlus.setRGBLight(RGBLight.RGBL, Color.OFF) 12 | basic.pause(1000) 13 | DFRobotMaqueenPlus.servoRun(Servos.S1, 90) 14 | basic.pause(1000) 15 | basic.forever(function () { 16 | serial.writeValue("Speed", DFRobotMaqueenPlus.readSpeed(Motors1.M1)) 17 | serial.writeValue("direction", DFRobotMaqueenPlus.readDirection(Motors1.M1)) 18 | serial.writeValue("patrol", DFRobotMaqueenPlus.readPatrol(Patrol.L1)) 19 | serial.writeValue("ultrasonic", DFRobotMaqueenPlus.ultraSonic(PIN.P0, PIN.P0)) 20 | serial.writeValue("voltage", DFRobotMaqueenPlus.readPatrolVoltage(Patrol.L1)) 21 | }) 22 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "noImplicitAny": true, 5 | "outDir": "built", 6 | "rootDir": "." 7 | }, 8 | "exclude": ["pxt_modules/**/*test.ts"] 9 | } 10 | --------------------------------------------------------------------------------