let servoleft = 0 let servomax = 0 let servodeadband = 0 let servoright = 0 let timeout = 0 let servocenter = 0 let rya = 0 let accelscale = 0 let ya = 0 let servomin = 0 let radio_rx_time = 0 let xa = 0 let channel = 0 let rxa = 0 let current_time = 0 let mode = 0 let b_event = 0 let a_event = 0 let state = 0 // Demo Remote Control of Parallax SumoBot example // // 1 August 2017 // // // All of the transmit/receive code is in one project // to demonstrate the power of the simulator. After // setting up the transmitter, the simulated receiver // will appear and can be setup. (Ignore the servos on // the simulated transmitter) // // // Setup // // -------------------- // // Basic Stamp is held in reset (output of reset // button wired to VSS - leg is the one closet to the // .com in www.parallax.com) // // Micro:bit is soldered to single row standard header // and inserted into the SumoBot's breadboard // // Power to Micro:bit is generated from VDD supply (I // used LP2950-3.3) // // // P0 is connected to the right servo on P12 // // P1 is connected to the left servo on P13 // // // Operation // // -------------------- // // After micro:bit boot // // // Pressing B toggles between transmit and receive // mode (flashing T or R) // // Pressing A advances to channel selection mode // // Pressing B increments channel number (allows // simultaneous operation of multiple // transmitter/receiver pairs) // // Pressing A commences operation (solid T or R // indicates operating mode) // // Tilt the micro:bit transmitter to operate the // remote SumoBot. Hopefully the controls are // intuitive. // // Pressing B on transmitter stops operation. Press B // again to resume. // // Receiver will timeout and stop if it doesn't // receive a packet at least once a second // // Use the reset button to reconfigure the device basic.forever(() => { // 0 is transmit selected state // // 1 is receive selected state // // 2 is channel select state // // 10 is transmit // // // Use B button to temporarily stop robot // // Use reset button to exit and reconfigure // // 11 is temporary stop // // 20 is receive // // // Use reset button to exit and reconfigure // if (state == 0) { mode = 0 basic.showLeds(` # # # # # . . # . . . . # . . . . # . . . . # . . `) basic.showLeds(` # # # # # # # # # # . # # # . . # # # . . # # # . `) if (a_event) { a_event = 0 state = 2 } if (b_event) { b_event = 0 state = 1 } } else if (state == 1) { mode = 1 basic.showLeds(` # # # . . # . . # . # # # . . # . . # . # . . . # `) basic.showLeds(` # # # . . # # . # . # # # . . # # . # . # # . . # `) if (a_event) { a_event = 0 state = 2 } if (b_event) { b_event = 0 state = 0 } } else if (state == 2) { basic.showNumber(channel) if (a_event) { a_event = 0 radio.setGroup(channel) if (mode == 0) { state = 10 basic.showString("T") } else { state = 20 rxa = 0 rya = 0 servoleft = servocenter servoright = servocenter basic.showString("R") } } if (b_event) { b_event = 0 // Limit # of possible channels to 4 to make UX // simpler? channel = (channel + 1) % 4 } } else if (state == 10) { if (b_event) { b_event = 0 state = 11 basic.showString("S") } xa = input.acceleration(Dimension.X) ya = input.acceleration(Dimension.Y) radio.sendValue("xa", xa) radio.sendValue("ya", ya) } else if (state == 11) { if (b_event) { b_event = 0 state = 10 basic.showString("T") } xa = 0 ya = 0 radio.sendValue("xa", xa) radio.sendValue("ya", ya) } else if (state == 20) { current_time = input.runningTime() if (current_time - radio_rx_time > timeout) { pins.servoWritePin(AnalogPin.P0, servocenter) pins.servoWritePin(AnalogPin.P1, servocenter) } else { pins.servoWritePin(AnalogPin.P0, servoright) pins.servoWritePin(AnalogPin.P1, servoleft) } } }) input.onButtonPressed(Button.A, () => { a_event = 1 }) input.onButtonPressed(Button.B, () => { b_event = 1 }) radio.onDataPacketReceived( ({ receivedString: received_string, receivedNumber: value }) => { if (received_string == "xa") { rxa = value / accelscale if (Math.abs(rxa) < servodeadband) { rxa = 0 } } if (received_string == "ya") { radio_rx_time = input.runningTime() rya = value / accelscale if (Math.abs(rya) < servodeadband) { rya = 0 } // We send data in rxa, rya order so update servo // settings after receiving rya servoleft = servocenter - rya servoleft = servoleft + rxa servoleft = Math.min(servoleft, servomax) servoleft = Math.max(servoleft, servomin) servoright = servocenter + rya servoright = servoright + rxa servoright = Math.min(servoright, servomax) servoright = Math.max(servoright, servomin) } }) state = 0 mode = 0 channel = 0 servomin = 54 servocenter = 90 servomax = 126 servodeadband = 5 accelscale = 28 radio_rx_time = 0 timeout = 1000 radio.setTransmitPower(7)