|
| 1 | + |
| 2 | +// Remote Control - As The Controller Device |
| 3 | +// |
| 4 | +// This script configures your Arduino to remotely control an OpenMV Cam using the RPC |
| 5 | +// library. |
| 6 | +// |
| 7 | +// This script is designed to pair with "popular_features_as_the_remote_device.py" running |
| 8 | +// on the OpenMV Cam. |
| 9 | + |
| 10 | +#include <openmvrpc.h> |
| 11 | + |
| 12 | +// The RPC library above provides mutliple classes for controlling an OpenMV Cam over |
| 13 | +// CAN, I2C, SPI, or Serial (UART). |
| 14 | + |
| 15 | +// We need to define a scratch buffer for holding messages. The maximum amount of data |
| 16 | +// you may pass in any on direction is limited to the size of this buffer minus 4. |
| 17 | + |
| 18 | +uint8_t scratch_buffer[256 + 4]; |
| 19 | + |
| 20 | +/////////////////////////////////////////////////////////////// |
| 21 | +// Choose the interface you wish to control an OpenMV Cam over. |
| 22 | +/////////////////////////////////////////////////////////////// |
| 23 | + |
| 24 | +// Uncomment the below line to setup your Arduino for controlling over CAN. |
| 25 | +// |
| 26 | +// * message_id - CAN message to use for data transport on the can bus (11-bit). |
| 27 | +// * bit_rate - CAN bit rate. |
| 28 | +// |
| 29 | +// NOTE: Master and slave message ids and can bit rates must match. Connect master can high to slave |
| 30 | +// can high and master can low to slave can lo. The can bus must be terminated with 120 ohms. |
| 31 | +// |
| 32 | +// openmv::rpc_can_master interface(message_id=0x7FF, bit_rate=250000, sampling_point=75); |
| 33 | + |
| 34 | +// Uncomment the below line to setup your Arduino for controlling over I2C. |
| 35 | +// |
| 36 | +// * slave_addr - I2C address. |
| 37 | +// * rate - I2C Bus Clock Frequency. |
| 38 | +// |
| 39 | +// NOTE: Master and slave addresses must match. Connect master scl to slave scl and master sda |
| 40 | +// to slave sda. You must use external pull ups. Finally, both devices must share a ground. |
| 41 | +// |
| 42 | +openmv::rpc_i2c_master interface(scratch_buffer, sizeof(scratch_buffer), 0x12, 100000); |
| 43 | + |
| 44 | +// Uncomment the below line to setup your Arduino for controlling over SPI. |
| 45 | +// |
| 46 | +// * cs_pin - Slave Select Pin. |
| 47 | +// * freq - SPI Bus Clock Frequency. |
| 48 | +// * spi_mode - See (https://www.arduino.cc/en/reference/SPI) |
| 49 | +// |
| 50 | +// NOTE: Master and slave settings much match. Connect CS, SCLK, MOSI, MISO to CS, SCLK, MOSI, MISO. |
| 51 | +// Finally, both devices must share a common ground. |
| 52 | +// |
| 53 | +// openmv::rpc_spi_master interface(scratch_buffer, sizeof(scratch_buffer), 10, 1000000, SPI_MODE2); |
| 54 | + |
| 55 | +// Uncomment the below line to setup your Arduino for controlling over a hardware UART. |
| 56 | +// |
| 57 | +// * baudrate - Serial Baudrate. |
| 58 | +// |
| 59 | +// NOTE: Master and slave baud rates must match. Connect master tx to slave rx and master rx to |
| 60 | +// slave tx. Finally, both devices must share a common ground. |
| 61 | +// |
| 62 | +// WARNING: The program and debug port for your Arduino may be "Serial". If so, you cannot use |
| 63 | +// "Serial" to connect to an OpenMV Cam without blocking your Arduino's ability to |
| 64 | +// be programmed and use print/println. |
| 65 | +// |
| 66 | +// openmv::rpc_hardware_serial_uart_master -> Serial |
| 67 | +// openmv::rpc_hardware_serial1_uart_master -> Serial1 |
| 68 | +// openmv::rpc_hardware_serial2_uart_master -> Serial2 |
| 69 | +// openmv::rpc_hardware_serial3_uart_master -> Serial3 |
| 70 | +// |
| 71 | +// openmv::rpc_hardware_serial1_uart_master interface(scratch_buffer, sizeof(scratch_buffer), 115200); |
| 72 | + |
| 73 | +// Uncomment the below line to setup your Arduino for controlling over a software UART. |
| 74 | +// |
| 75 | +// * rx_pin - RX Pin (See the reference guide about what pins can be used) |
| 76 | +// * tx_pin - TX Pin (see the reference guide about what pins can be used) |
| 77 | +// * baudrate - Serial Baudrate (See the reference guide https://www.arduino.cc/en/Reference/SoftwareSerial) |
| 78 | +// |
| 79 | +// NOTE: Master and slave baud rates must match. Connect master tx to slave rx and master rx to |
| 80 | +// slave tx. Finally, both devices must share a common ground. |
| 81 | +// |
| 82 | +// openmv::rpc_software_serial_uart_master interface(scratch_buffer, sizeof(scratch_buffer), 2, 3, 19200); |
| 83 | + |
| 84 | +void setup() { |
| 85 | + Serial.begin(115200); |
| 86 | +} |
| 87 | + |
| 88 | +////////////////////////////////////////////////////////////// |
| 89 | +// Call Back Handlers |
| 90 | +////////////////////////////////////////////////////////////// |
| 91 | + |
| 92 | +void exe_face_detection() |
| 93 | +{ |
| 94 | + struct { uint16_t x, y, w, h; } face_detection_result; |
| 95 | + if (interface.call_no_args(F("face_detection"), &face_detection_result, sizeof(face_detection_result))) { |
| 96 | + Serial.print(F("Largest Face Detected [x=")); |
| 97 | + Serial.print(face_detection_result.x); |
| 98 | + Serial.print(F(", y=")); |
| 99 | + Serial.print(face_detection_result.y); |
| 100 | + Serial.print(F(", w=")); |
| 101 | + Serial.print(face_detection_result.w); |
| 102 | + Serial.print(F(", h=")); |
| 103 | + Serial.print(face_detection_result.h); |
| 104 | + Serial.println(F("]")); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void exe_person_detection() |
| 109 | +{ |
| 110 | + char buff[32 + 1] = {}; // null terminator |
| 111 | + if (interface.call_no_args(F("person_detection"), buff, sizeof(buff) - 1)) { |
| 112 | + Serial.println(buff); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void exe_qrcode_detection() |
| 117 | +{ |
| 118 | + char buff[128 + 1] = {}; // null terminator |
| 119 | + if (interface.call_no_args(F("qrcode_detection"), buff, sizeof(buff) - 1)) { |
| 120 | + Serial.println(buff); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void exe_apriltag_detection() |
| 125 | +{ |
| 126 | + struct { uint16_t cx, cy, id, rot; } apriltag_detection_result; |
| 127 | + if (interface.call_no_args(F("apriltag_detection"), &apriltag_detection_result, sizeof(apriltag_detection_result))) { |
| 128 | + Serial.print(F("Largest Tag Detected [cx=")); |
| 129 | + Serial.print(apriltag_detection_result.cx); |
| 130 | + Serial.print(F(", cy=")); |
| 131 | + Serial.print(apriltag_detection_result.cy); |
| 132 | + Serial.print(F(", id=")); |
| 133 | + Serial.print(apriltag_detection_result.id); |
| 134 | + Serial.print(F(", rot=")); |
| 135 | + Serial.print(apriltag_detection_result.rot); |
| 136 | + Serial.println(F("]")); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +void exe_datamatrix_detection() |
| 141 | +{ |
| 142 | + char buff[128 + 1] = {}; // null terminator |
| 143 | + if (interface.call_no_args(F("datamatrix_detection"), buff, sizeof(buff) - 1)) { |
| 144 | + Serial.println(buff); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +void exe_barcode_detection() |
| 149 | +{ |
| 150 | + char buff[128 + 1] = {}; // null terminator |
| 151 | + if (interface.call_no_args(F("barcode_detection"), buff, sizeof(buff) - 1)) { |
| 152 | + Serial.println(buff); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +void exe_color_detection() |
| 157 | +{ |
| 158 | + int8_t color_thresholds[6] = {30, 100, 15, 127, 15, 127}; // generic_red_thresholds |
| 159 | + // int8_t color_thresholds[6] = {30, 100, -64, -8, -32, 32}; // generic_green_thresholds |
| 160 | + // int8_t color_thresholds[6] = {0, 30, 0, 64, -128, 0}; // generic_blue_thresholds |
| 161 | + struct { uint16_t cx, cy; } color_detection_result; |
| 162 | + if (interface.call(F("color_detection"), color_thresholds, sizeof(color_thresholds), &color_detection_result, sizeof(color_detection_result))) { |
| 163 | + Serial.print(F("Largest Color Detected [cx=")); |
| 164 | + Serial.print(color_detection_result.cx); |
| 165 | + Serial.print(F(", cy=")); |
| 166 | + Serial.print(color_detection_result.cy); |
| 167 | + Serial.println(F("]")); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// Execute remote functions in a loop. Please choose and uncomment one remote function below. |
| 172 | +// Executing multiple at a time may run slowly if the camera needs to change camera modes |
| 173 | +// per execution. |
| 174 | + |
| 175 | +void loop() { |
| 176 | + exe_face_detection(); // Face should be about 2ft away. |
| 177 | + // exe_person_detection(); |
| 178 | + // exe_qrcode_detection(); // Place the QRCode about 2ft away. |
| 179 | + // exe_apriltag_detection(); |
| 180 | + // exe_datamatrix_detection(); // Place the Datamatrix about 2ft away. |
| 181 | + // exe_barcode_detection(); // Place the Barcode about 2ft away. |
| 182 | + // exe_color_detection(); |
| 183 | +} |
0 commit comments