Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ tasks:
- cd scratch-prg-extensions/extensions/src/arduino_basics && pnpm install
- ln -s $PWD/scratch-prg-extensions/extensions/src/arduino_modulino $PWD/prg-raise-playground/extensions/src/arduino_modulino
- cd scratch-prg-extensions/extensions/src/arduino_modulino && pnpm install
- ln -s $PWD/scratch-prg-extensions/extensions/src/arduino_include_robot $PWD/prg-raise-playground/extensions/src/arduino_include_robot
- cd scratch-prg-extensions/extensions/src/arduino_include_robot && pnpm install

app:upload:
desc: "Build,and Upload zip file to Arduino board, unzip and deploy to /home/arduino/ArduinoApps"
Expand Down Expand Up @@ -84,7 +86,7 @@ tasks:

scratch:watch:
cmds:
- cd prg-raise-playground && pnpm dev -i arduino_basics arduino_modulino
- cd prg-raise-playground && pnpm dev -i arduino_basics arduino_modulino arduino_include_robot

python:watch:
desc: "Watch Python folder for changes and auto-upload/restart"
Expand Down
1 change: 1 addition & 0 deletions python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def on_matrix_draw(_, data):


ui.on_message("matrix_draw", on_matrix_draw)
ui.on_message("robot_forward_for_steps", lambda _, data: Bridge.call("robot_forward_for_steps", data.get("steps")))


def on_modulino_button_pressed(btn):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createTestSuite } from "$testing";
import Extension from ".";

createTestSuite({ Extension, __dirname }, {
unitTests: undefined,
integrationTests: undefined,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
type Environment,
extension,
scratch
} from "$common";

// import { io, type Socket } from "socket.io-client";

// Get Arduino board IP or hostname from URL parameter
const getArduinoBoardHost = () => {
const urlParams = new URLSearchParams(window.location.search);
const boardHost = urlParams.get("host");
if (boardHost) {
return boardHost;
}
return window.location.hostname;
};

export default class IncludeRobot extends extension({
name: "Include Robot",
description: "A programmable robot designed to teach junior students about robotics in a simple and funny way.",
// iconURL: "include-robot.png", // png
// insetIconURL: "include-robot-buttons.svg", // svg
tags: ["Arduino UNO Q"],
blockColor: "#00878F",
menuColor: "#8C7965",
menuSelectColor: "#62AEB2",
}) {
// private socket: Socket | null = null;

init(env: Environment) {
const arduinoBoardHost = getArduinoBoardHost();
var serverURL = `wss://${arduinoBoardHost}:7000`;

// this.socket = io(serverURL, {
// path: "/socket.io",
// transports: ["polling", "websocket"],
// autoConnect: true,
// });

// this.socket.on("connect", () => {
// console.log(`Connected to Arduino UNO Q`, serverURL);
// });

// this.socket.on("disconnect", (reason) => {
// console.log(`Disconnected from Arduino UNO Q: ${reason}`);
// });
}

@(scratch.command`Move robot forward for ${"number"} steps`)
robotForwardForSteps(steps: number) {
if (this.socket) {
this.socket.emit("robot_forward_for_steps", { steps: steps });
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "arduino_include_robot-extension",
"version": "1.0.0",
"description": "An extension created using the PRG AI Blocks framework",
"main": "index.ts",
"scripts": {
"directory": "echo arduino_include_robot",
"test": "pnpm --filter prg-extension-root test arduino_include_robot/index.test.ts",
"dev": "pnpm --filter prg-extension-root dev --include arduino_include_robot",
"add:ui": "pnpm --filter prg-extension-root add:ui arduino_include_robot",
"add:arg": "pnpm --filter prg-extension-root add:arg arduino_include_robot"
},
"author": "",
"license": "ISC",
"dependencies": {
"socket.io-client": "^4.8.1"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions sketch/robot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "robot.h"
#include <Servo.h>


void Robot::moveForward(const int steps, const uint16_t for_ms) {
for (int i = 0; i < steps; i++) {
this->rightWheel.write(90 + this->speed);
this->leftWheel.write(90 - this->speed);
delay(for_ms);
this->rightWheel.write(90);
this->leftWheel.write(90);
delay(500);
}
}

void Robot::moveBackward(const int steps, const uint16_t for_ms) {
for (int i = 0; i < steps; i++) {
this->rightWheel.write(90 - this->speed);
this->leftWheel.write(90 + this->speed);
delay(for_ms);
this->rightWheel.write(90);
this->leftWheel.write(90);
delay(500);
}
}

void Robot::turnRight(const uint16_t for_ms) {
this->rightWheel.write(90 + 10);
this->leftWheel.write(90 + 10);
delay(for_ms);
this->rightWheel.write(90);
this->leftWheel.write(90);
}

void Robot::turnLeft(const uint16_t for_ms) {
this->rightWheel.write(90 - 10);
this->leftWheel.write(90 - 10);
delay(for_ms);
this->rightWheel.write(90);
this->leftWheel.write(90);
}

// set the speed of the robot. From 0 (no speed) to 100 (max speed)
void Robot::setSpeed(int speed) {
speed = constrain(speed, 0, 100);
speed = map(speed, 0, 100, 0, 90);
this->speed = speed;
}
26 changes: 26 additions & 0 deletions sketch/robot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <Arduino.h>
#include <Servo.h>

class Robot {
Servo rightWheel;
Servo leftWheel;
int speed;

public:
Robot() {} // Empty constructor
Robot(int rightWheelPin, int leftWheelPin) {
this->rightWheel = Servo();
this->leftWheel = Servo();
this->rightWheel.attach(rightWheelPin);
this->leftWheel.attach(leftWheelPin);
this->setSpeed(10);
}

void setSpeed(const int steps);
void moveForward(const int steps, const uint16_t for_ms);
void moveBackward(const int steps, const uint16_t for_ms);
void turnRight(const uint16_t for_ms);
void turnLeft(const uint16_t for_ms);
};
10 changes: 10 additions & 0 deletions sketch/sketch.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include <Arduino_RouterBridge.h>
#include "Arduino_LED_Matrix.h"
#include <Arduino_Modulino.h>
#include "robot.h"

Arduino_LED_Matrix matrix;
ModulinoButtons buttons;

uint8_t rservo = 9;
uint8_t lservo = 8;
Robot myra = Robot(rservo, lservo);

void setup() {
matrix.begin();
Bridge.begin();
Expand All @@ -28,6 +33,11 @@ void setup() {
buttons.setLeds(true, true, true);
Bridge.provide("matrix_draw", matrix_draw);
Bridge.provide("set_led_rgb", set_led_rgb);
Bridge.provide("robot_forward_for_steps", robot_forward_for_steps);
}

void robot_forward_for_steps(uint8_t steps){
myra.moveForward(steps, 1000);
}

void loop() {
Expand Down
1 change: 1 addition & 0 deletions sketch/sketch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ profiles:
- STM32duino VL53L4ED (1.0.1)
- Arduino_LSM6DSOX (1.1.2)
- Arduino_LPS22HB (1.0.2)
- Servo (1.3.0)
default_profile: default
Loading