Skip to content

Commit 0a65c78

Browse files
authored
Merge pull request #1 from arduino-libraries/devel
0.3.0
2 parents 654489d + 3246c65 commit 0a65c78

File tree

9 files changed

+421
-32
lines changed

9 files changed

+421
-32
lines changed
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
This file is part of the Arduino_Alvik library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
13+
/*
14+
You can use this sketch to make your Arduino Nano ESP32 an USB-to-serial adapter.
15+
This allows to flash firmware into the Arduino Alvik Carrier via Arduino Nano ESP32.
16+
17+
Please refer to Arduino_AlvikCarrier library available at https://github.com/arduino-libraries/Arduino_AlvikCarrier
18+
*/
19+
20+
21+
22+
23+
#include "Arduino_Alvik.h"
24+
25+
unsigned long baud = 115200;
26+
27+
int rts = -1;
28+
int dtr = -1;
29+
30+
31+
static void onLineChange(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
32+
if (event_base == ARDUINO_USB_CDC_EVENTS) {
33+
arduino_usb_cdc_event_data_t * data = (arduino_usb_cdc_event_data_t*)event_data;
34+
switch (event_id) {
35+
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
36+
auto baud = data->line_coding.bit_rate;
37+
Serial0.updateBaudRate(baud);
38+
while (Serial0.available()) {
39+
Serial0.read();
40+
}
41+
break;
42+
}
43+
}
44+
}
45+
46+
void setup() {
47+
Serial.onEvent(onLineChange);
48+
Serial.enableReboot(false);
49+
Serial.begin(baud);
50+
Serial.setRxBufferSize(0);
51+
Serial.setRxBufferSize(2048);
52+
Serial0.setRxBufferSize(8192);
53+
Serial0.setTxBufferSize(8192);
54+
Serial0.begin(baud, SERIAL_8E1);
55+
Serial0.flush();
56+
pinMode(BOOT_STM32, OUTPUT);
57+
pinMode(RESET_STM32, OUTPUT);
58+
digitalWrite(BOOT_STM32,HIGH);
59+
delay(1000);
60+
digitalWrite(RESET_STM32,LOW);
61+
delay(1000);
62+
digitalWrite(RESET_STM32,HIGH);
63+
64+
65+
66+
}
67+
68+
void loop() {
69+
int len = 0;
70+
uint8_t auc_buffer[488];
71+
while (Serial.available() && len < sizeof(auc_buffer)) {
72+
auc_buffer[len++] = Serial.read();
73+
}
74+
if (len) {
75+
Serial0.write(auc_buffer, len);
76+
}
77+
78+
len = 0;
79+
while (Serial0.available() && len < sizeof(auc_buffer)) {
80+
auc_buffer[len++] = Serial0.read();
81+
}
82+
if (len) {
83+
Serial.write(auc_buffer, len);
84+
}
85+
}

Diff for: examples/color_calibration/color_calibration.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void setup() {
3636
}
3737

3838
void loop() {
39-
int16_t color[3];
39+
int color[3];
4040
float rgb[3];
4141
float hsv[3];
4242
alvik.get_color_raw(color[0], color[1], color[2]);

Diff for: examples/drive/drive.ino

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
This file is part of the Arduino_Alvik library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include "Arduino_Alvik.h"
13+
14+
Arduino_Alvik alvik;
15+
16+
void setup() {
17+
alvik.begin();
18+
}
19+
20+
void loop() {
21+
alvik.drive(10, 45);
22+
delay(10000);
23+
alvik.drive(10, -45);
24+
delay(10000);
25+
}

Diff for: examples/line_follower/line_follower.ino

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
This file is part of the Arduino_Alvik library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include "Arduino_Alvik.h"
13+
14+
Arduino_Alvik alvik;
15+
16+
int line_sensors[3];
17+
float error = 0;
18+
float control = 0;
19+
float kp = 50.0;
20+
21+
22+
23+
void setup() {
24+
Serial.begin(115200);
25+
while((!Serial)&&(millis()>3000));
26+
alvik.begin();
27+
alvik.left_led.set_color(0,0,1);
28+
alvik.right_led.set_color(0,0,1);
29+
30+
while(!alvik.get_touch_ok()){
31+
delay(50);
32+
}
33+
}
34+
35+
void loop() {
36+
while (!alvik.get_touch_cancel()){
37+
38+
alvik.get_line_sensors(line_sensors[0], line_sensors[1], line_sensors[2]);
39+
Serial.print(line_sensors[0]);
40+
Serial.print("\t");
41+
Serial.print(line_sensors[1]);
42+
Serial.print("\t");
43+
Serial.print(line_sensors[2]);
44+
Serial.print("\n");
45+
error = calculate_center(line_sensors[0], line_sensors[1], line_sensors[2]);
46+
control = error * kp;
47+
if (control > 0.2){
48+
alvik.left_led.set_color(1,0,0);
49+
alvik.right_led.set_color(0,0,0);
50+
}
51+
else{
52+
if (control < -0.2){
53+
alvik.left_led.set_color(0,0,0);
54+
alvik.right_led.set_color(1,0,0);
55+
}
56+
else{
57+
alvik.left_led.set_color(0,1,0);
58+
alvik.right_led.set_color(0,1,0);
59+
}
60+
}
61+
62+
alvik.set_wheels_speed(30-control, 30+control);
63+
delay(100);
64+
}
65+
66+
while (!alvik.get_touch_ok()){
67+
alvik.left_led.set_color(0,0,1);
68+
alvik.right_led.set_color(0,0,1);
69+
alvik.brake();
70+
delay(100);
71+
}
72+
}
73+
74+
float calculate_center(const int left, const int center, const int right){
75+
float centroid = 0.0;
76+
float sum_weight = left + center + right;
77+
float sum_values = left + center * 2 + right * 3;
78+
if (sum_weight!=0.0){ // divide by zero protection
79+
centroid=sum_values/sum_weight;
80+
centroid=-centroid+2.0; // so it is right on robot axis Y
81+
}
82+
return centroid;
83+
}

Diff for: examples/remote_control/remote_control.ino

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
This file is part of the Arduino_Alvik library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
// This example shows how to interface 2 Alvik robots via ESPnow.
13+
// At startup, you can select if an Alvik is a trasmitter by pressing the "check button" or a receiver by pressing "cancel button". Use arrows to move the robot.
14+
15+
#include "Arduino_Alvik.h"
16+
#include <esp_now.h>
17+
#include <WiFi.h>
18+
19+
Arduino_Alvik alvik;
20+
21+
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
22+
uint8_t myData;
23+
esp_now_peer_info_t peerInfo;
24+
25+
26+
int alvik_mode = -1; // 0 is receiver, 1 is sender
27+
28+
bool led_blink = false;
29+
30+
31+
void setup() {
32+
Serial.begin(115200);
33+
while((!Serial)&&(millis()>3000));
34+
35+
alvik.begin();
36+
37+
WiFi.mode(WIFI_STA);
38+
if (esp_now_init() != ESP_OK) {
39+
Serial.println("Error initializing ESP-NOW");
40+
return;
41+
}
42+
43+
while (alvik_mode == -1){
44+
if (alvik.get_touch_cancel()){
45+
alvik_mode = 0;
46+
}
47+
if (alvik.get_touch_ok()){
48+
alvik_mode = 1;
49+
}
50+
}
51+
if (alvik_mode == 0){
52+
esp_now_register_recv_cb(OnDataRecv);
53+
}
54+
else{
55+
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
56+
peerInfo.channel = 0;
57+
peerInfo.encrypt = false;
58+
59+
if (esp_now_add_peer(&peerInfo) != ESP_OK){
60+
Serial.println("Failed to add peer");
61+
return;
62+
}
63+
}
64+
}
65+
66+
void loop() {
67+
if (alvik_mode==0){
68+
alvik.left_led.set_color(led_blink, !led_blink, 0);
69+
alvik.right_led.set_color(!led_blink, led_blink, 0);
70+
delay(500);
71+
}
72+
else{
73+
if (alvik.get_touch_any()){
74+
if (alvik.get_touch_up()){
75+
myData = 'F';
76+
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
77+
}
78+
if (alvik.get_touch_down()){
79+
myData = 'B';
80+
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
81+
}
82+
if (alvik.get_touch_left()){
83+
myData = 'L';
84+
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
85+
}
86+
if (alvik.get_touch_right()){
87+
myData = 'R';
88+
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
89+
}
90+
if (alvik.get_touch_center()){
91+
myData = 'S';
92+
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
93+
}
94+
}
95+
alvik.left_led.set_color(0, 0, led_blink);
96+
alvik.right_led.set_color(0, 0, led_blink);
97+
delay(100);
98+
}
99+
led_blink = !led_blink;
100+
}
101+
102+
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len){
103+
Serial.print(incomingData[0]);
104+
switch (incomingData[0]){
105+
case 'F':
106+
alvik.drive(7, 0);
107+
break;
108+
case 'B':
109+
alvik.drive(-7, 0);
110+
break;
111+
case 'L':
112+
alvik.drive(0, 45);
113+
break;
114+
case 'R':
115+
alvik.drive(0, -45);
116+
break;
117+
case 'S':
118+
alvik.brake();
119+
break;
120+
}
121+
Serial.println();
122+
}

Diff for: library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Arduino_Alvik
2-
version=0.2.0
2+
version=0.3.0
33
author=Arduino, Giovanni di Dio Bruno, Lucio Rossi
44
maintainer=Arduino <[email protected]>
55
sentence=Library to code Arduino Alvik robot

0 commit comments

Comments
 (0)