Skip to content

Commit c507709

Browse files
committed
added corner ultrasonic sensor positions + adjusted speed of robot
1 parent f534175 commit c507709

File tree

8 files changed

+75
-116
lines changed

8 files changed

+75
-116
lines changed

demo/src/index.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ body {
6767

6868

6969

70-
canvas {
70+
canvas {
7171
height : auto;
72-
width: 100%;
72+
width: 100%;
7373
max-width: 900px;
74+
7475
}
7576

7677

7778
.robot-environment{
7879
margin: 20px;
80+
7981
}
8082

demo/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ function initiateRobot(){
136136
const canvas = document.getElementById('world');
137137

138138
const robot = new Robots.Arduino.TwoServoRobot(canvas)
139-
robot.addUltrasonicSensor('L2', 2,3);
139+
robot.addUltrasonicSensor('CFL', 2,3);
140140
robot.addUltrasonicSensor('F2', 4,5);
141+
robot.addUltrasonicSensor('CFR', 2,3);
142+
robot.addUltrasonicSensor('CBR', 4,5);robot.addUltrasonicSensor('CFL', 2,3);
143+
robot.addUltrasonicSensor('CBL', 4,5);robot.addUltrasonicSensor('CFL', 2,3);
141144
robot.environment.setRobotInitialPosition({x:50, y: 100});
142145
robot.environment?.addObstacleRectangle(400, 50, 800, 20, "grey");
143146
robot.environment?.addObstacleRectangle(700, 200, 20, 800, "grey");

demo/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"target": "es2018",
55
"baseUrl": "..",
66
"rootDir": "..",
7-
"lib": ["dom"],
7+
"lib": ["dom", "ES2015"],
88
"paths": {
99
"p4robots": ["src"]
1010
}

src/Arduino/Uno/execute.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
CPU,
55
timer0Config,
66
timer1Config,
7+
timer2Config,
78
AVRIOPort,
89
AVRUSART,
910
portBConfig,
@@ -12,7 +13,7 @@ import {
1213
usart0Config
1314
} from 'avr8js';
1415
import { loadHex } from './intelhex';
15-
import { MicroTaskScheduler } from './task-scheduler';
16+
1617
type Event = {
1718
period : number, //milliseconds
1819
eventCall : any
@@ -26,13 +27,13 @@ export class AVRRunner {
2627
readonly cpu: CPU;
2728
readonly timer0: AVRTimer;
2829
readonly timer1: AVRTimer;
30+
readonly timer2: AVRTimer;
2931
readonly portB: AVRIOPort;
3032
readonly portC: AVRIOPort;
3133
readonly portD: AVRIOPort;
3234
readonly usart: AVRUSART;
3335
readonly speed = 16e6; // 16 MHZ
3436
readonly workUnitCycles = 500000;
35-
readonly taskScheduler = new MicroTaskScheduler();
3637

3738

3839
//events
@@ -44,11 +45,11 @@ export class AVRRunner {
4445
this.cpu = new CPU(this.program);
4546
this.timer0 = new AVRTimer(this.cpu, timer0Config);
4647
this.timer1 = new AVRTimer(this.cpu, timer1Config);
48+
this.timer2 = new AVRTimer(this.cpu, timer2Config);
4749
this.portB = new AVRIOPort(this.cpu, portBConfig);
4850
this.portC = new AVRIOPort(this.cpu, portCConfig);
4951
this.portD = new AVRIOPort(this.cpu, portDConfig);
5052
this.usart = new AVRUSART(this.cpu, usart0Config, this.speed);
51-
this.taskScheduler.start();
5253
}
5354

5455
private cpuTimeMS = 0;

src/Arduino/Uno/task-scheduler.spec.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/Arduino/Uno/task-scheduler.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/ArduinoRobotEnvironment/TwoServoRobot.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class TwoServoRobot {
2828

2929
//add arduino events
3030
//update the wheel speeds from servo components
31-
this.arduino.addCPUEvent(5, () => {
31+
this.arduino.addCPUEvent(100, () => {
3232
const leftServoSpeed = (this.servoLeft.getWidthOfLastPulse() - 1.4);
3333
const rightServoSpeed = -1*(this.servoRight.getWidthOfLastPulse() - 1.4);
3434
this.environment?.setSpeeds(leftServoSpeed, rightServoSpeed);
@@ -43,12 +43,11 @@ export class TwoServoRobot {
4343
this.ultrasonicSensors[key].sensor.setDistanceOfObstacle(this.environment.ultrasonicSensorDistances[key]);
4444
}
4545

46+
this.environment?.tick(50);
4647

47-
})
48-
this.arduino.addCPUEvent(50, () => {
49-
this.environment?.tick(1000);
5048

5149
})
50+
5251
this.arduino.addCPUEventMicrosecond(5, (cpuCycles : number) => {
5352
if(this.environment)
5453
{
@@ -77,7 +76,6 @@ export class TwoServoRobot {
7776
run(hex: string)
7877
{
7978
this.environment?.reset();
80-
this.environment?.tick(100);
8179
for(const key in this.ultrasonicSensors){
8280
this.ultrasonicSensors[key].sensor.setDistanceOfObstacle(this.environment.ultrasonicSensorDistances[key]);
8381
}

src/robots/TwoWheelRobot.ts

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
import {Body, Engine, Render, Bodies, World, Vector, Runner, Events, Mouse, MouseConstraint} from "matter-js";
1+
import {
2+
Body,
3+
Engine,
4+
Render,
5+
Bodies,
6+
World,
7+
Vector,
8+
Runner,
9+
Events,
10+
Mouse,
11+
MouseConstraint,
12+
Composite
13+
} from "matter-js";
214
import {createPartCircle} from "./utils/CustomBodies"
315
import { getTranformedPoint, findMinimumDistanceToObstacle } from "./utils/utils";
416

517
type event = (robot : TwoWheelRobot) => any;
6-
export type sensorPosition = 'L1'| 'L2' | 'L3' | 'F1' | 'F2' | 'F3' | 'R1' | 'R2' | 'R3' | 'B1' | 'B2'| 'B3';
18+
export type sensorPosition = 'L1'| 'L2' | 'L3' | 'F1' | 'F2' | 'F3' | 'R1' | 'R2' | 'R3' | 'B1' | 'B2'| 'B3' | 'CFL' | 'CFR' | 'CBL' | 'CBR';
719
export class TwoWheelRobot {
820
private _canvas : any;
921
private _engine : Engine;
@@ -17,8 +29,8 @@ export class TwoWheelRobot {
1729
robotInitialAngle : number;
1830

1931
robotMass : number = 1000;
20-
robotFrictionAir:number = 0.5;
21-
static readonly forceMultiplier = 0.0015;
32+
robotFrictionAir:number = 1;
33+
static readonly forceMultiplier = 0.5;
2234

2335
ultrasonicSensor : Body;
2436
static readonly maxUltrasonicDistance = 400;
@@ -34,7 +46,12 @@ export class TwoWheelRobot {
3446
'F3': {x: 14, y: 9, angle: 0},
3547
'B1': {x: -14, y:-9, angle: -Math.PI},
3648
'B2': {x: -14, y: 0, angle: -Math.PI},
37-
'B3': {x: -14, y: 9, angle: -Math.PI}
49+
'B3': {x: -14, y: 9, angle: -Math.PI},
50+
'CFL': {x: 14, y:-9, angle: Math.PI/4},
51+
'CFR': {x: 14, y: 9, angle: -Math.PI/4},
52+
'CBL': {x: -14, y:-9, angle: -5*Math.PI/4},
53+
'CBR': {x: -14, y: 9, angle: 5*Math.PI/4},
54+
3855
}
3956
ultrasonicSensorDistances : { [position: string]: number; } = {};
4057

@@ -99,6 +116,19 @@ export class TwoWheelRobot {
99116

100117
World.add(this._engine.world, [this.robot, ]);//obstacle]);
101118

119+
/*
120+
// add mouse control
121+
const mouse = Mouse.create(this._canvas);
122+
123+
const mouseConstraint = MouseConstraint.create(this._engine, {
124+
mouse: mouse
125+
});
126+
127+
World.add(this._engine.world, mouseConstraint);
128+
// keep the mouse in sync with rendering
129+
this._render.mouse = mouse;
130+
*/
131+
102132
Render.run(this._render);
103133
this.reset();
104134

@@ -130,8 +160,17 @@ export class TwoWheelRobot {
130160

131161
}
132162
}
163+
/*
164+
//Add event with 'mousemove'
165+
Events.on(mouseConstraint, 'mousemove', function (event) {
166+
//For Matter.Query.point pass "array of bodies" and "mouse position"
167+
168+
//Your custom code here
169+
console.log(event); //returns a shape corrisponding to the mouse position
133170
171+
});
134172
173+
*/
135174
self.updateUltrasonicSensor();
136175

137176

@@ -208,24 +247,30 @@ export class TwoWheelRobot {
208247
this.leftWheelSpeed = left;
209248
this.rightWheelSpeed = right;
210249
}
211-
private once = 0;
250+
//TODO review if this is actually solving a bug or not
251+
private starting = true;
212252
applyForces() : void {
213-
253+
if(!this.starting) {
214254
const leftForcePosition = getTranformedPoint(this.robotBody.position, this.robot.angle, 0, -10);
215255
const rightForcePosition = getTranformedPoint(this.robotBody.position, this.robot.angle, 0, 10);
216-
let leftWheelForce = Vector.create(TwoWheelRobot.forceMultiplier*Math.abs(this.leftWheelSpeed), 0);
256+
let leftWheelForce = Vector.create(TwoWheelRobot.forceMultiplier * Math.abs(this.leftWheelSpeed), 0);
217257
leftWheelForce = Vector.rotate(leftWheelForce, this.robot.angle);
218-
if(this.leftWheelSpeed < 0)
258+
if (this.leftWheelSpeed < 0)
219259
leftWheelForce = Vector.neg(leftWheelForce);
220260

221-
let rightWheelForce = Vector.create(TwoWheelRobot.forceMultiplier*Math.abs(this.rightWheelSpeed), 0);
261+
let rightWheelForce = Vector.create(TwoWheelRobot.forceMultiplier * Math.abs(this.rightWheelSpeed), 0);
222262
rightWheelForce = Vector.rotate(rightWheelForce, this.robot.angle);
223-
if(this.rightWheelSpeed < 0)
263+
if (this.rightWheelSpeed < 0)
224264
rightWheelForce = Vector.neg(rightWheelForce);
225265

226-
Body.applyForce(this.robot, rightForcePosition, rightWheelForce);
227266
Body.applyForce(this.robot, leftForcePosition, leftWheelForce);
267+
Body.applyForce(this.robot, rightForcePosition, rightWheelForce);
268+
}
228269

270+
else
271+
{
272+
this.starting = false;
273+
}
229274

230275
}
231276

@@ -253,6 +298,7 @@ export class TwoWheelRobot {
253298
Body.setAngle(this.robot, this.robotInitialAngle);
254299
Body.setVelocity(this.robot, {x:0,y:0});
255300
Body.setAngularVelocity(this.robot,0);
301+
Body.update(this.robot, 10, 1,1);
256302
this.leftWheelSpeed = 0;
257303
this.rightWheelSpeed = 0;
258304
for(const coin of this.removedCoins)
@@ -261,7 +307,7 @@ export class TwoWheelRobot {
261307
}
262308
this.removedCoins = [];
263309

264-
this.tick(10);
310+
//this.tick(10);
265311
this.updateUltrasonicSensor();
266312

267313
}

0 commit comments

Comments
 (0)