Skip to content

Commit 01f48cf

Browse files
Merge pull request #2109 from arduino/Pedromsousalima/Alvik/Add_C_API_Reference
MKC-1768 - Add multi-language information to API reference (ALVIK)
2 parents 71734af + 9347022 commit 01f48cf

File tree

8 files changed

+485
-9
lines changed

8 files changed

+485
-9
lines changed

content/hardware/08.edu/solution-and-kits/alvik/tutorials/api-overview/api-overview.md

+300-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ hardware:
1313
## API List
1414

1515
To access to any of these functions you need first to initialize an instance of the class **ArduinoAlvik()**.
16-
16+
This reference is useful for both **MicroPython** and **C++ environments** as the functions were all created to have the same form across development environments meaning your experience should be easy to carry both options.
1717

1818
```arduino
1919
alvik = ArduinoAlvik()
@@ -33,7 +33,7 @@ _Returns true if robot is on_
3333

3434
**Outputs**
3535

36-
- boolean: Returns true if robot is on, false if is off.
36+
- boolean: Returns true if robot is on, false if it is off.
3737

3838
### `begin`
3939

@@ -75,7 +75,7 @@ _Returns the orientation of the IMU_
7575

7676
get_accelerations()
7777

78-
_Returns the 3-axial acceleration of the IMU_
78+
_Returns the 3-axial acceleration values of the IMU_
7979

8080
**Outputs**
8181

@@ -136,7 +136,7 @@ _Returns last acknowledgement_
136136

137137
**Outputs**
138138

139-
- **last_ack**: last acknowledgement value
139+
- **last_ack**: last acknowledgement value.
140140

141141
### `get_battery_charge`
142142

@@ -146,7 +146,7 @@ _Returns the battery SOC_
146146

147147
**Outputs**
148148

149-
- **battery_soc**: percentage of charge
149+
- **battery_soc**: percentage of charge.
150150

151151
### `get_touch_any`
152152

@@ -633,7 +633,7 @@ _Register callback when touch button RIGHT is pressed_
633633
- **callback**: the name of the function to recall
634634
- **args**: optional arguments of the function
635635

636-
## Extras
636+
## Function Parameters
637637

638638
### The Distance Unit
639639

@@ -677,6 +677,298 @@ Rotational speed unit of measurement used in the APIs:
677677

678678
### `"blocking" or "non blocking"`
679679

680-
While programming a microcontroller, the terms "blocking" means that **all the resources are used only in performing a specific action, and no other things can happen at the same time**. Usually this is used when you want to be precise or you don't want anything else that could interact with the action you are performing.
680+
While programming a microcontroller, the term "blocking" means that **all the resources are used only in performing a specific action, and no other things can happen at the same time**. Usually this is used when you want to be precise or you don't want anything else that could interact with the action you are performing.
681+
682+
On the other hand, "Non blocking", means that the microcontroller is free to do other things while the action is been performed.
683+
684+
## Examples
685+
These examples demonstrate practical implementations on how to use the Arduino Alvik API. Whether you're working with MicroPython or C++, these simple examples will help you understand how to implement various features in your projects, making it easy to get started with the Alvik in the language you're most comfortable with.
686+
687+
### Simple Color Sensing Example
688+
689+
This example demonstrates how to implement basic color sensing. The Alvik's color sensor reads the color of an object placed under it, and the detected color is printed to the console.
690+
691+
- **Reference for MicroPython**
692+
693+
```python
694+
from arduino_alvik import ArduinoAlvik
695+
from time import sleep_ms
696+
697+
alvik = ArduinoAlvik()
698+
alvik.begin()
699+
700+
print("Alvik initialized for color sensing")
701+
702+
while True:
703+
color = alvik.get_color_label()
704+
print(f"Detected color: {color}")
705+
sleep_ms(500)
706+
```
707+
708+
- **Reference for C++**
709+
710+
```c
711+
#include "Arduino_Alvik.h"
712+
713+
Arduino_Alvik alvik;
714+
715+
void setup() {
716+
Serial.begin(9600);
717+
alvik.begin();
718+
Serial.println("Alvik initialized for color sensing");
719+
}
720+
721+
void loop() {
722+
char* color = alvik.get_color_label();
723+
Serial.print("Detected color: ");
724+
Serial.println(color);
725+
delay(500);
726+
}
727+
```
728+
729+
730+
731+
732+
### Simple Directional Control
733+
734+
This example demonstrates very basic control over the robot's movement based on directional arrow buttons. The Alvik will drive in the direction corresponding to the arrow button pressed (up, down, left, right).
735+
736+
- **Reference for MicroPython**
737+
```python
738+
from arduino_alvik import ArduinoAlvik
739+
from time import sleep_ms
740+
741+
alvik = ArduinoAlvik()
742+
alvik.begin()
743+
744+
print("Alvik initialized")
745+
746+
while True:
747+
if alvik.get_touch_up():
748+
print("Moving Up")
749+
alvik.drive(100, 0, linear_unit='cm/s')
750+
sleep_ms(1000)
751+
alvik.brake()
752+
elif alvik.get_touch_down():
753+
print("Moving Down")
754+
alvik.drive(-100, 0, linear_unit='cm/s')
755+
sleep_ms(1000)
756+
alvik.brake()
757+
elif alvik.get_touch_left():
758+
print("Turning Left")
759+
alvik.drive(0, 100, angular_unit='deg/s')
760+
sleep_ms(1000)
761+
alvik.brake()
762+
elif alvik.get_touch_right():
763+
print("Turning Right")
764+
alvik.drive(0, -100, angular_unit='deg/s')
765+
sleep_ms(1000)
766+
alvik.brake()
767+
sleep_ms(100)
768+
769+
```
770+
- **Reference for C++**
771+
772+
```c
773+
#include "Arduino_Alvik.h"
774+
775+
Arduino_Alvik alvik;
776+
777+
void setup() {
778+
Serial.begin(9600);
779+
alvik.begin();
780+
Serial.println("Alvik initialized");
781+
}
782+
783+
void loop() {
784+
if (alvik.get_touch_up()) {
785+
Serial.println("Moving Up");
786+
alvik.drive(100, 0, "cm/s");
787+
delay(1000);
788+
alvik.brake();
789+
} else if (alvik.get_touch_down()) {
790+
Serial.println("Moving Down");
791+
alvik.drive(-100, 0, "cm/s");
792+
delay(1000);
793+
alvik.brake();
794+
} else if (alvik.get_touch_left()) {
795+
Serial.println("Turning Left");
796+
alvik.drive(0, 100, "deg/s");
797+
delay(1000);
798+
alvik.brake();
799+
} else if (alvik.get_touch_right()) {
800+
Serial.println("Turning Right");
801+
alvik.drive(0, -100, "deg/s");
802+
delay(1000);
803+
alvik.brake();
804+
}
805+
delay(100);
806+
}
807+
```
808+
809+
### Line Following
810+
811+
This example demonstrates how to create a simple line-following robot. The code initializes the Alvik, reads sensor data to detect the line, calculates the error from the center of the line, and adjusts the Alvik's wheel speeds to follow the line. It also uses LEDs to indicate the direction the robot is turning.
812+
813+
The Alvik starts when the OK `` button is pressed and stops when the Cancel button is pressed. The Alvik continuously reads the line sensors, calculates the error, and adjusts the wheel speeds to correct its path.
814+
815+
- **Reference for MicroPython**
816+
817+
```python
818+
from arduino_alvik import ArduinoAlvik
819+
from time import sleep_ms
820+
import sys
821+
822+
823+
def calculate_center(left: int, center: int, right: int):
824+
centroid = 0
825+
sum_weight = left + center + right
826+
sum_values = left + 2 * center + 3 * right
827+
if sum_weight != 0:
828+
centroid = sum_values / sum_weight
829+
centroid = 2 - centroid
830+
return centroid
831+
832+
833+
alvik = ArduinoAlvik()
834+
alvik.begin()
835+
836+
error = 0
837+
control = 0
838+
kp = 50.0
839+
840+
alvik.left_led.set_color(0, 0, 1)
841+
alvik.right_led.set_color(0, 0, 1)
842+
843+
while alvik.get_touch_ok():
844+
sleep_ms(50)
845+
846+
while not alvik.get_touch_ok():
847+
sleep_ms(50)
848+
849+
try:
850+
while True:
851+
while not alvik.get_touch_cancel():
852+
853+
line_sensors = alvik.get_line_sensors()
854+
print(f' {line_sensors}')
855+
856+
error = calculate_center(*line_sensors)
857+
control = error * kp
858+
859+
if control > 0.2:
860+
alvik.left_led.set_color(1, 0, 0)
861+
alvik.right_led.set_color(0, 0, 0)
862+
elif control < -0.2:
863+
alvik.left_led.set_color(1, 0, 0)
864+
alvik.right_led.set_color(0, 0, 0)
865+
else:
866+
alvik.left_led.set_color(0, 1, 0)
867+
alvik.right_led.set_color(0, 1, 0)
868+
869+
alvik.set_wheels_speed(30 - control, 30 + control)
870+
sleep_ms(100)
871+
872+
while not alvik.get_touch_ok():
873+
alvik.left_led.set_color(0, 0, 1)
874+
alvik.right_led.set_color(0, 0, 1)
875+
alvik.brake()
876+
sleep_ms(100)
877+
878+
except KeyboardInterrupt as e:
879+
print('over')
880+
alvik.stop()
881+
sys.exit()
882+
```
883+
884+
- **Reference for C++**
885+
886+
```c
887+
/*
888+
This file is part of the Arduino_Alvik library.
889+
890+
Copyright (c) 2024 Arduino SA
891+
892+
This Source Code Form is subject to the terms of the Mozilla Public
893+
License, v. 2.0. If a copy of the MPL was not distributed with this
894+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
895+
896+
*/
897+
898+
#include "Arduino_Alvik.h"
899+
900+
Arduino_Alvik alvik;
901+
902+
int line_sensors[3];
903+
float error = 0;
904+
float control = 0;
905+
float kp = 50.0;
906+
907+
908+
909+
void setup() {
910+
Serial.begin(115200);
911+
while((!Serial)&&(millis()>3000));
912+
alvik.begin();
913+
alvik.left_led.set_color(0,0,1);
914+
alvik.right_led.set_color(0,0,1);
915+
916+
while(!alvik.get_touch_ok()){
917+
delay(50);
918+
}
919+
}
920+
921+
void loop() {
922+
while (!alvik.get_touch_cancel()){
923+
924+
alvik.get_line_sensors(line_sensors[0], line_sensors[1], line_sensors[2]);
925+
Serial.print(line_sensors[0]);
926+
Serial.print("\t");
927+
Serial.print(line_sensors[1]);
928+
Serial.print("\t");
929+
Serial.print(line_sensors[2]);
930+
Serial.print("\n");
931+
error = calculate_center(line_sensors[0], line_sensors[1], line_sensors[2]);
932+
control = error * kp;
933+
if (control > 0.2){
934+
alvik.left_led.set_color(1,0,0);
935+
alvik.right_led.set_color(0,0,0);
936+
}
937+
else{
938+
if (control < -0.2){
939+
alvik.left_led.set_color(0,0,0);
940+
alvik.right_led.set_color(1,0,0);
941+
}
942+
else{
943+
alvik.left_led.set_color(0,1,0);
944+
alvik.right_led.set_color(0,1,0);
945+
}
946+
}
947+
948+
alvik.set_wheels_speed(30-control, 30+control);
949+
delay(100);
950+
}
951+
952+
while (!alvik.get_touch_ok()){
953+
alvik.left_led.set_color(0,0,1);
954+
alvik.right_led.set_color(0,0,1);
955+
alvik.brake();
956+
delay(100);
957+
}
958+
}
959+
960+
float calculate_center(const int left, const int center, const int right){
961+
float centroid = 0.0;
962+
float sum_weight = left + center + right;
963+
float sum_values = left + center * 2 + right * 3;
964+
if (sum_weight!=0.0){ // divide by zero protection
965+
centroid=sum_values/sum_weight;
966+
centroid=-centroid+2.0; // so it is right on robot axis Y
967+
}
968+
return centroid;
969+
}
970+
```
971+
972+
973+
681974
682-
On the other hand, "Non blocking", means that the microcontroller is free to do other thing while the action is been performed.

content/hardware/08.edu/solution-and-kits/alvik/tutorials/getting-started/getting-started.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ Now that you have played with Alvik and have seen it moving, it is time to know
2929

3030
### Let's Start Coding Alvik
3131

32-
Alvik is intended to be programmed with MicroPyton. We recommend you to install the [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython) editor. Follow the instructions next to the download link to install it and open the IDE
32+
Alvik is intended to be programmed with MicroPyton. We recommend you to install the [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython) editor. Follow the instructions next to the download link to install it and open the IDE.
33+
34+
Alternatively, on par with other Arduino products, you can also program your Alvik using Arduino IDE and C++. If this is the case you can find setup instructions over at [Setting up Alvik on Arduino IDE](../setting-alvik-arduino-ide/setting-alvik-arduino-ide.md).
3335

3436
Now that all the previous steps have been set, let's see how to make Alvik moving across your room while avoiding objects! Let's create custom program for Alvik that:
3537

0 commit comments

Comments
 (0)