aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-08-21 23:52:53 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-08-21 23:52:53 +0200
commit4255e0ad78e0f7cc8694df77e585ca02a8113bb3 (patch)
tree0b1c1b28922e14d642b53e116043ad2c03ef6bc1
parent2de5220071f515ac59261d1e40b076701f40923a (diff)
...
-rw-r--r--car/car.ino33
-rw-r--r--car/elegoo.pb.c18
-rw-r--r--car/elegoo.pb.h38
-rw-r--r--car/elegoo/elegoo.pb.go70
-rw-r--r--car/elegoo/elegoo.proto12
-rw-r--r--car/elegoo/main.go63
6 files changed, 114 insertions, 120 deletions
diff --git a/car/car.ino b/car/car.ino
index 474d543..0bcc293 100644
--- a/car/car.ino
+++ b/car/car.ino
@@ -4,7 +4,6 @@
#include <PacketSerial.h>
#include <Servo.h>
#include <IRremote.h>
-//#include <os48.h>
#include <pb_encode.h>
#include <pb_decode.h>
@@ -14,7 +13,6 @@
PacketSerial serial;
Servo servo;
-int trim;
IRrecv irrecv(IR);
decode_results ir;
@@ -32,13 +30,10 @@ void motor(int e, int a, int b, int v) {
analogWrite(e, v);
}
-void motorR(int v) {
- motor(ENA, IN1, IN2, v);
-}
-
-void motorL(int v) {
- motor(ENB, IN3, IN4, v);
-}
+#define motorR(v) motor(ENA, IN1, IN2, v)
+#define motorL(v) motor(ENB, IN3, IN4, v)
+#define look(deg) servo.write(deg + 90)
+#define looking() (servo.read() - 90)
int distance() {
digitalWrite(Trig, LOW);
@@ -49,12 +44,6 @@ int distance() {
return pulseIn(Echo, HIGH, TimeOut) / ToCM;
}
-// + left
-// - right
-void look(int deg) {
- servo.write(90 + deg + trim);
-}
-
void onPacket(const uint8_t* buf, size_t size) {
Command cmd = Command_init_zero;
@@ -71,16 +60,12 @@ void onPacket(const uint8_t* buf, size_t size) {
motorR(0);
motorL(0);
}
- if (cmd.has_Trim) {
- trim = cmd.Trim;
- }
- if (cmd.has_TurnHead) {
- look(cmd.TurnHead);
+ if (cmd.has_Direction) {
+ look(cmd.Direction);
}
if (cmd.has_Center) {
look(0);
}
-
}
void env() {
@@ -106,6 +91,9 @@ void env() {
irrecv.resume();
}
+ evt.Direction = looking();
+ evt.has_Direction = true;
+
pb_ostream_t ostream = pb_ostream_from_buffer(buf, sizeof(buf));
pb_encode_delimited(&ostream, Event_fields, &evt);
@@ -115,7 +103,7 @@ void env() {
void loop() {
env();
serial.update();
- delay(200);
+ delay(100);
}
void setup() {
@@ -142,4 +130,5 @@ void setup() {
irrecv.enableIRIn();
servo.attach(SRV);
+ servo.write(90);
}
diff --git a/car/elegoo.pb.c b/car/elegoo.pb.c
index e9a762d..7ec3892 100644
--- a/car/elegoo.pb.c
+++ b/car/elegoo.pb.c
@@ -1,5 +1,5 @@
/* Automatically generated nanopb constant definitions */
-/* Generated by nanopb-0.3.6 at Mon Aug 21 23:14:33 2017. */
+/* Generated by nanopb-0.3.6 at Mon Aug 21 23:37:01 2017. */
#include "elegoo.pb.h"
@@ -10,22 +10,22 @@
-const pb_field_t Command_fields[7] = {
+const pb_field_t Command_fields[6] = {
PB_FIELD( 1, SINT32 , OPTIONAL, STATIC , FIRST, Command, SpeedR, SpeedR, 0),
PB_FIELD( 2, SINT32 , OPTIONAL, STATIC , OTHER, Command, SpeedL, SpeedR, 0),
PB_FIELD( 3, BOOL , OPTIONAL, STATIC , OTHER, Command, Stop, SpeedL, 0),
PB_FIELD( 4, BOOL , OPTIONAL, STATIC , OTHER, Command, Center, Stop, 0),
- PB_FIELD( 5, SINT32 , OPTIONAL, STATIC , OTHER, Command, TurnHead, Center, 0),
- PB_FIELD( 6, SINT32 , OPTIONAL, STATIC , OTHER, Command, Trim, TurnHead, 0),
+ PB_FIELD( 5, SINT32 , OPTIONAL, STATIC , OTHER, Command, Direction, Center, 0),
PB_LAST_FIELD
};
-const pb_field_t Event_fields[6] = {
+const pb_field_t Event_fields[7] = {
PB_FIELD( 1, UINT32 , OPTIONAL, STATIC , FIRST, Event, Distance, Distance, 0),
- PB_FIELD( 2, BOOL , OPTIONAL, STATIC , OTHER, Event, SensorR, Distance, 0),
- PB_FIELD( 3, BOOL , OPTIONAL, STATIC , OTHER, Event, SensorC, SensorR, 0),
- PB_FIELD( 4, BOOL , OPTIONAL, STATIC , OTHER, Event, SensorL, SensorC, 0),
- PB_FIELD( 5, UINT32 , OPTIONAL, STATIC , OTHER, Event, KeyPress, SensorL, 0),
+ PB_FIELD( 2, SINT32 , OPTIONAL, STATIC , OTHER, Event, Direction, Distance, 0),
+ PB_FIELD( 3, BOOL , OPTIONAL, STATIC , OTHER, Event, SensorR, Direction, 0),
+ PB_FIELD( 4, BOOL , OPTIONAL, STATIC , OTHER, Event, SensorC, SensorR, 0),
+ PB_FIELD( 5, BOOL , OPTIONAL, STATIC , OTHER, Event, SensorL, SensorC, 0),
+ PB_FIELD( 6, UINT32 , OPTIONAL, STATIC , OTHER, Event, KeyPress, SensorL, 0),
PB_LAST_FIELD
};
diff --git a/car/elegoo.pb.h b/car/elegoo.pb.h
index 0518f9f..a82e03f 100644
--- a/car/elegoo.pb.h
+++ b/car/elegoo.pb.h
@@ -1,5 +1,5 @@
/* Automatically generated nanopb header */
-/* Generated by nanopb-0.3.6 at Mon Aug 21 23:14:33 2017. */
+/* Generated by nanopb-0.3.6 at Mon Aug 21 23:37:01 2017. */
#ifndef PB_ELEGOO_PB_H_INCLUDED
#define PB_ELEGOO_PB_H_INCLUDED
@@ -24,16 +24,16 @@ typedef struct _Command {
bool Stop;
bool has_Center;
bool Center;
- bool has_TurnHead;
- int32_t TurnHead;
- bool has_Trim;
- int32_t Trim;
+ bool has_Direction;
+ int32_t Direction;
/* @@protoc_insertion_point(struct:Command) */
} Command;
typedef struct _Event {
bool has_Distance;
uint32_t Distance;
+ bool has_Direction;
+ int32_t Direction;
bool has_SensorR;
bool SensorR;
bool has_SensorC;
@@ -48,31 +48,31 @@ typedef struct _Event {
/* Default values for struct fields */
/* Initializer values for message structs */
-#define Command_init_default {false, 0, false, 0, false, 0, false, 0, false, 0, false, 0}
-#define Event_init_default {false, 0, false, 0, false, 0, false, 0, false, 0}
-#define Command_init_zero {false, 0, false, 0, false, 0, false, 0, false, 0, false, 0}
-#define Event_init_zero {false, 0, false, 0, false, 0, false, 0, false, 0}
+#define Command_init_default {false, 0, false, 0, false, 0, false, 0, false, 0}
+#define Event_init_default {false, 0, false, 0, false, 0, false, 0, false, 0, false, 0}
+#define Command_init_zero {false, 0, false, 0, false, 0, false, 0, false, 0}
+#define Event_init_zero {false, 0, false, 0, false, 0, false, 0, false, 0, false, 0}
/* Field tags (for use in manual encoding/decoding) */
#define Command_SpeedR_tag 1
#define Command_SpeedL_tag 2
#define Command_Stop_tag 3
#define Command_Center_tag 4
-#define Command_TurnHead_tag 5
-#define Command_Trim_tag 6
+#define Command_Direction_tag 5
#define Event_Distance_tag 1
-#define Event_SensorR_tag 2
-#define Event_SensorC_tag 3
-#define Event_SensorL_tag 4
-#define Event_KeyPress_tag 5
+#define Event_Direction_tag 2
+#define Event_SensorR_tag 3
+#define Event_SensorC_tag 4
+#define Event_SensorL_tag 5
+#define Event_KeyPress_tag 6
/* Struct field encoding specification for nanopb */
-extern const pb_field_t Command_fields[7];
-extern const pb_field_t Event_fields[6];
+extern const pb_field_t Command_fields[6];
+extern const pb_field_t Event_fields[7];
/* Maximum encoded size of messages (where known) */
-#define Command_size 28
-#define Event_size 18
+#define Command_size 22
+#define Event_size 24
/* Message IDs (where set with "msgid" option) */
#ifdef PB_MSGID
diff --git a/car/elegoo/elegoo.pb.go b/car/elegoo/elegoo.pb.go
index b20c09c..0d779ee 100644
--- a/car/elegoo/elegoo.pb.go
+++ b/car/elegoo/elegoo.pb.go
@@ -29,12 +29,11 @@ var _ = math.Inf
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type Command struct {
- SpeedR int32 `protobuf:"zigzag32,1,opt,name=SpeedR" json:"SpeedR,omitempty"`
- SpeedL int32 `protobuf:"zigzag32,2,opt,name=SpeedL" json:"SpeedL,omitempty"`
- Stop bool `protobuf:"varint,3,opt,name=Stop" json:"Stop,omitempty"`
- Center bool `protobuf:"varint,4,opt,name=Center" json:"Center,omitempty"`
- TurnHead int32 `protobuf:"zigzag32,5,opt,name=TurnHead" json:"TurnHead,omitempty"`
- Trim int32 `protobuf:"zigzag32,6,opt,name=Trim" json:"Trim,omitempty"`
+ SpeedR int32 `protobuf:"zigzag32,1,opt,name=SpeedR" json:"SpeedR,omitempty"`
+ SpeedL int32 `protobuf:"zigzag32,2,opt,name=SpeedL" json:"SpeedL,omitempty"`
+ Stop bool `protobuf:"varint,3,opt,name=Stop" json:"Stop,omitempty"`
+ Center bool `protobuf:"varint,4,opt,name=Center" json:"Center,omitempty"`
+ Direction int32 `protobuf:"zigzag32,5,opt,name=Direction" json:"Direction,omitempty"`
}
func (m *Command) Reset() { *m = Command{} }
@@ -70,26 +69,20 @@ func (m *Command) GetCenter() bool {
return false
}
-func (m *Command) GetTurnHead() int32 {
+func (m *Command) GetDirection() int32 {
if m != nil {
- return m.TurnHead
- }
- return 0
-}
-
-func (m *Command) GetTrim() int32 {
- if m != nil {
- return m.Trim
+ return m.Direction
}
return 0
}
type Event struct {
- Distance uint32 `protobuf:"varint,1,opt,name=Distance" json:"Distance,omitempty"`
- SensorR bool `protobuf:"varint,2,opt,name=SensorR" json:"SensorR,omitempty"`
- SensorC bool `protobuf:"varint,3,opt,name=SensorC" json:"SensorC,omitempty"`
- SensorL bool `protobuf:"varint,4,opt,name=SensorL" json:"SensorL,omitempty"`
- KeyPress uint32 `protobuf:"varint,5,opt,name=KeyPress" json:"KeyPress,omitempty"`
+ Distance uint32 `protobuf:"varint,1,opt,name=Distance" json:"Distance,omitempty"`
+ Direction int32 `protobuf:"zigzag32,2,opt,name=Direction" json:"Direction,omitempty"`
+ SensorR bool `protobuf:"varint,3,opt,name=SensorR" json:"SensorR,omitempty"`
+ SensorC bool `protobuf:"varint,4,opt,name=SensorC" json:"SensorC,omitempty"`
+ SensorL bool `protobuf:"varint,5,opt,name=SensorL" json:"SensorL,omitempty"`
+ KeyPress uint32 `protobuf:"varint,6,opt,name=KeyPress" json:"KeyPress,omitempty"`
}
func (m *Event) Reset() { *m = Event{} }
@@ -104,6 +97,13 @@ func (m *Event) GetDistance() uint32 {
return 0
}
+func (m *Event) GetDirection() int32 {
+ if m != nil {
+ return m.Direction
+ }
+ return 0
+}
+
func (m *Event) GetSensorR() bool {
if m != nil {
return m.SensorR
@@ -140,19 +140,19 @@ func init() {
func init() { proto.RegisterFile("elegoo.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
- // 219 bytes of a gzipped FileDescriptorProto
- 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x90, 0xb1, 0x4a, 0xc5, 0x30,
- 0x14, 0x86, 0x89, 0xb6, 0x69, 0x09, 0x76, 0x30, 0x83, 0x04, 0xa7, 0xd2, 0xa9, 0x93, 0x8b, 0x6f,
- 0x60, 0x15, 0x04, 0x3b, 0x48, 0xda, 0xc9, 0x2d, 0xda, 0x83, 0x14, 0x4c, 0x52, 0x92, 0x28, 0xf8,
- 0x10, 0x8e, 0xbe, 0xef, 0x25, 0x87, 0x34, 0xbd, 0xdb, 0xf9, 0xfe, 0x93, 0xfc, 0xf9, 0x08, 0xbb,
- 0x82, 0x2f, 0xf8, 0xb4, 0xf6, 0x6e, 0x73, 0x36, 0xd8, 0xee, 0x9f, 0xb0, 0x6a, 0xb0, 0x5a, 0x2b,
- 0xb3, 0xf0, 0x1b, 0x46, 0xa7, 0x0d, 0x60, 0x91, 0x82, 0xb4, 0xa4, 0xbf, 0x96, 0x89, 0x72, 0x3e,
- 0x8a, 0x8b, 0xb3, 0x7c, 0xe4, 0x9c, 0x15, 0x53, 0xb0, 0x9b, 0xb8, 0x6c, 0x49, 0x5f, 0x4b, 0x9c,
- 0xe3, 0xd9, 0x01, 0x4c, 0x00, 0x27, 0x0a, 0x4c, 0x13, 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 0x34, 0xf5,
+ 0x76, 0x14, 0xd7, 0xc0, 0xbb, 0x19, 0x71, 0xd4, 0x92, 0x95, 0xac, 0xbe, 0xd2, 0x89, 0xb6, 0x5c,
+ 0xc9, 0xc3, 0x9f, 0x5c, 0x09, 0x01, 0x59, 0x17, 0x68, 0x96, 0x67, 0x25, 0xab, 0x0b, 0x1d, 0xdf,
+ 0xcb, 0xdf, 0x06, 0x6d, 0x40, 0x27, 0xb3, 0x98, 0x26, 0x12, 0xb7, 0x70, 0xde, 0x1a, 0x87, 0x43,
+ 0x30, 0x64, 0xe5, 0x31, 0xce, 0xec, 0x41, 0xf5, 0xcd, 0xe0, 0xf8, 0xf8, 0x81, 0x36, 0x88, 0x1b,
+ 0x28, 0x5a, 0xe3, 0x43, 0x6f, 0x07, 0x8c, 0x16, 0x97, 0x7a, 0xe3, 0xff, 0x1b, 0x87, 0x93, 0x0d,
+ 0x21, 0x21, 0xef, 0xd0, 0x7a, 0x72, 0x3a, 0x09, 0xad, 0xb8, 0x37, 0x4d, 0x92, 0x5a, 0x71, 0x6f,
+ 0x54, 0x74, 0xda, 0x1a, 0xb5, 0x78, 0x3c, 0xe1, 0xe7, 0xb3, 0x43, 0xef, 0x25, 0xff, 0xf5, 0x58,
+ 0xf9, 0x81, 0xbf, 0x64, 0x53, 0x6f, 0xec, 0x2b, 0x8f, 0x27, 0xbc, 0xff, 0x09, 0x00, 0x00, 0xff,
+ 0xff, 0x78, 0x67, 0x1b, 0x88, 0x52, 0x01, 0x00, 0x00,
}
diff --git a/car/elegoo/elegoo.proto b/car/elegoo/elegoo.proto
index bdf1b95..f9b13bf 100644
--- a/car/elegoo/elegoo.proto
+++ b/car/elegoo/elegoo.proto
@@ -7,14 +7,14 @@ message Command {
sint32 SpeedL = 2;
bool Stop = 3;
bool Center = 4;
- sint32 TurnHead = 5; // ±90 deg
- sint32 Trim = 6;
+ sint32 Direction = 5; // +90 left; -90 right
}
message Event {
uint32 Distance = 1;
- bool SensorR = 2;
- bool SensorC = 3;
- bool SensorL = 4;
- uint32 KeyPress = 5;
+ sint32 Direction = 2;
+ bool SensorR = 3;
+ bool SensorC = 4;
+ bool SensorL = 5;
+ uint32 KeyPress = 6;
}
diff --git a/car/elegoo/main.go b/car/elegoo/main.go
index df5dabc..e2f4e05 100644
--- a/car/elegoo/main.go
+++ b/car/elegoo/main.go
@@ -7,6 +7,7 @@ import (
"bufio"
"io"
"log"
+ "time"
"github.com/dim13/cobs"
"github.com/golang/protobuf/proto"
@@ -49,38 +50,42 @@ func main() {
defer s.Close()
go func() {
- cmd := &Command{}
- cmd.Trim = -10
- cmd.Center = true
- Write(s, cmd)
-
- /* MOTOR
- cmd.SpeedL = 200
- cmd.SpeedR = 0
- cmd.Stop = false
- Write(s, cmd)
- time.Sleep(3 * time.Second)
-
- cmd.SpeedL = 0
- cmd.SpeedR = 0
- cmd.Stop = true
- Write(s, cmd)
- time.Sleep(3 * time.Second)
-
- cmd.SpeedL = 0
- cmd.SpeedR = 200
- cmd.Stop = false
- Write(s, cmd)
- time.Sleep(3 * time.Second)
-
- cmd.SpeedL = 0
- cmd.SpeedR = 0
- cmd.Stop = true
- Write(s, cmd)
- */
+ time.Sleep(5 * time.Second)
+ Write(s, &Command{Direction: -45})
+ time.Sleep(time.Second)
+
+ Write(s, &Command{Direction: 45})
+ time.Sleep(time.Second)
+
+ Write(s, &Command{Center: true})
}()
+ /* MOTOR
+ cmd.SpeedL = 200
+ cmd.SpeedR = 0
+ cmd.Stop = false
+ Write(s, cmd)
+ time.Sleep(3 * time.Second)
+
+ cmd.SpeedL = 0
+ cmd.SpeedR = 0
+ cmd.Stop = true
+ Write(s, cmd)
+ time.Sleep(3 * time.Second)
+
+ cmd.SpeedL = 0
+ cmd.SpeedR = 200
+ cmd.Stop = false
+ Write(s, cmd)
+ time.Sleep(3 * time.Second)
+
+ cmd.SpeedL = 0
+ cmd.SpeedR = 0
+ cmd.Stop = true
+ Write(s, cmd)
+ */
+
buf := bufio.NewReader(s)
for {
evt := &Event{}