smalrubot 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8c5524cac06724e181f71cccf31f0d06cae2d0c
4
- data.tar.gz: 9c652f66ed763b167ea638acb9db33d6da93e8c5
3
+ metadata.gz: 642c4e0694f576d6a69d08cec9b88262e6b29da0
4
+ data.tar.gz: 424fcb96c73280e5910923462386932739104b29
5
5
  SHA512:
6
- metadata.gz: 978c66f080b78fc21c1f25d5dfe36db9c29c1638ef6d716c0ef16efe400fcc98e10372ad4a8a76abfa686b183fe256f2564b288cd046b45578f33f8ca817cdc2
7
- data.tar.gz: 81cae31984c60a1ce5a960ea8e95a308c872ea10870a21da0dd10cf4247b7b57162ab6139866d06dc30540106330a851fed49dedd99d41315e37c0ae885182e2
6
+ metadata.gz: 1dd5f9fdef4308f4dc800f838d232283ca95a128a4e348e56b26f073e6f16d1da85557355989cc0be54bffb39deb5580a6d089a02983b882b7fe1449ebe56f07
7
+ data.tar.gz: c40522501b506db212fb971db73f3b622fe70e15e32cd9106b3cc262ad131843d79bb7029a3156f7c27a3e04127dbf1c9ba1a6661ecc0690a1ab30ae66f7f271
data/.gitignore CHANGED
@@ -20,3 +20,4 @@ test/tmp
20
20
  test/version_tmp
21
21
  tmp
22
22
  /sr
23
+ /sr_studu
data/bin/smalrubot CHANGED
@@ -14,7 +14,11 @@ def usage
14
14
  $stderr.puts "Usage: #{File.basename($0)} COMMAND [command-specific-options]"
15
15
  $stderr.puts
16
16
  $stderr.puts "Commands:"
17
- $stderr.puts " generate-sketch [options]"
17
+ $stderr.puts " generate-sketch SKETCH [options]"
18
+ $stderr.puts
19
+ $stderr.puts " Sketches:"
20
+ $stderr.puts " sr"
21
+ $stderr.puts " sr_studu"
18
22
  $stderr.puts
19
23
  $stderr.puts " Options:"
20
24
  $stderr.puts " --baud BAUD"
@@ -25,11 +29,15 @@ end
25
29
 
26
30
  # Command must be the first argument.
27
31
  $options[:command] = ARGV.shift
28
- usage if $options[:command].match /help/
32
+ usage if !$options[:command] || $options[:command].match(/help/)
29
33
 
30
34
  # Parse the rest loosely.
31
35
  loop do
32
36
  case ARGV[0]
37
+ when 'sr'
38
+ $options[:sketch_names] << ARGV.shift
39
+ when 'sr_studu'
40
+ $options[:sketch_names] << ARGV.shift
33
41
  when '--baud'
34
42
  ARGV.shift; $options[:baud] = ARGV.shift
35
43
  when '--debug'
@@ -40,12 +48,10 @@ loop do
40
48
  end
41
49
  end
42
50
 
43
- $options[:sketch_names] = ['sr']
44
-
45
51
  error "Invalid command '#{$options[:command]}'" unless $options[:command] == "generate-sketch"
46
52
  error "No sketches or invalid sketches specified" if $options[:sketch_names].empty?
47
53
 
48
- $options[:sketch_names].each do |sketch_name|
54
+ $options[:sketch_names].uniq.each do |sketch_name|
49
55
  # Define the sources.
50
56
  sketch_dir = sketch_name
51
57
  sketch_file = sketch_name + ".ino"
@@ -61,7 +67,7 @@ $options[:sketch_names].each do |sketch_name|
61
67
 
62
68
  # Modify them based on the arguments.
63
69
  if $options[:baud]
64
- sketch.gsub! "115200", $options[:baud]
70
+ sketch.gsub! "19200", $options[:baud]
65
71
  end
66
72
  if $options[:debug]
67
73
  header.gsub! "// #define debug true", "#define debug true"
@@ -79,4 +85,10 @@ $options[:sketch_names].each do |sketch_name|
79
85
  File.open(dest_header, 'w') { |f| f.write header }
80
86
  File.open(dest_implementation, 'w') { |f| f.write implementation }
81
87
  File.open(dest_sketch, 'w') { |f| f.write sketch }
88
+
89
+ if sketch_name == 'sr_studu'
90
+ %w(SrStuduino.cpp SrStuduino.h).each do |name|
91
+ FileUtils.cp(File.join(src_dir, 'lib', name), File.join(dest_dir, name))
92
+ end
93
+ end
82
94
  end
data/examples/test.rb CHANGED
@@ -5,7 +5,7 @@ board = Smalrubot::Board.new(Smalrubot::TxRx::Serial.new)
5
5
 
6
6
  =begin
7
7
  board.digital_write(13, 255)
8
- sleep(1)
8
+ sleep(5)
9
9
  board.digital_write(13, 0)
10
10
  =end
11
11
 
@@ -0,0 +1,33 @@
1
+ module Smalrubot
2
+ class Board
3
+ # namespace for Studuino
4
+ module Studuino
5
+ PORT_M1 = 0
6
+ PORT_M2 = 1
7
+
8
+ PORT_A0 = 0
9
+ PORT_A1 = 1
10
+ PORT_A2 = 2
11
+ PORT_A3 = 3
12
+ PORT_A4 = 4
13
+ PORT_A5 = 5
14
+ PORT_A6 = 6
15
+ PORT_A7 = 7
16
+
17
+ PIDOPEN = 0
18
+ PIDLED = 1
19
+ PIDBUZZER = 2
20
+ PIDLIGHTSENSOR = 3
21
+ PIDSOUNDSENSOR = 4
22
+ PIDIRPHOTOREFLECTOR = 5
23
+ PIDACCELEROMETER = 6
24
+ PIDTOUCHSENSOR = 7
25
+ PIDPUSHSWITCH = 8
26
+
27
+ NORMAL = 0
28
+ REVERSE = 1
29
+ BRAKE = 2
30
+ COAST = 3
31
+ end
32
+ end
33
+ end
@@ -38,7 +38,14 @@ module Smalrubot
38
38
  digital_write: '01',
39
39
  analog_write: '03',
40
40
  servo_toggle: '08',
41
- servo_write: '09'
41
+ servo_write: '09',
42
+
43
+ set_dc_motor_calibration: '20',
44
+ init_dc_motor_port: '22',
45
+ init_sensor_port: '25',
46
+
47
+ dc_motor_power: '42',
48
+ dc_motor_control: '43',
42
49
  }
43
50
 
44
51
  WRITE_COMMANDS.each_key do |command|
@@ -51,6 +58,10 @@ module Smalrubot
51
58
  READ_COMMANDS = {
52
59
  digital_read: '02',
53
60
  analog_read: '04',
61
+
62
+ get_touch_sensor_value: '61',
63
+ get_light_sensor_value: '62',
64
+ get_ir_photoreflector_value: '64',
54
65
  }
55
66
 
56
67
  READ_COMMANDS.each_key do |command|
@@ -3,7 +3,7 @@ require 'rubyserial'
3
3
  module Smalrubot
4
4
  module TxRx
5
5
  class Serial < Base
6
- BAUD = 115200
6
+ BAUD = 19_200
7
7
 
8
8
  def initialize(options={})
9
9
  @device = options[:device]
@@ -25,7 +25,9 @@ module Smalrubot
25
25
  return super
26
26
  rescue BoardNotFound
27
27
  @tty_devices.shift
28
- @io.close
28
+ if @io
29
+ @io.close
30
+ end
29
31
  @io = nil
30
32
  end
31
33
  end
@@ -1,3 +1,3 @@
1
1
  module Smalrubot
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/smalrubot.rb CHANGED
@@ -2,6 +2,7 @@ require 'smalrubot/board_not_found'
2
2
  require 'smalrubot/version'
3
3
  require 'smalrubot/tx_rx'
4
4
  require 'smalrubot/board'
5
+ require 'smalrubot/board/studuino'
5
6
  require 'smalrubot/components'
6
7
 
7
8
  module Smalrubot
@@ -33,6 +33,19 @@ void Smalrubot::process() {
33
33
  Serial.print("Value - "); Serial.println(valStr);
34
34
  #endif
35
35
 
36
+ processCommand();
37
+
38
+ // Write the response.
39
+ if (response[0] != '\0') writeResponse();
40
+
41
+ #ifdef debug
42
+ Serial.print("Responded with - "); Serial.println(response);
43
+ Serial.println();
44
+ #endif
45
+ }
46
+
47
+
48
+ void Smalrubot::processCommand() {
36
49
  // Call the command.
37
50
  switch(cmd) {
38
51
  case 0: setMode (); break;
@@ -45,18 +58,9 @@ void Smalrubot::process() {
45
58
  case 90: reset (); break;
46
59
  default: break;
47
60
  }
48
-
49
- // Write the response.
50
- if (response[0] != '\0') writeResponse();
51
-
52
- #ifdef debug
53
- Serial.print("Responded with - "); Serial.println(response);
54
- Serial.println();
55
- #endif
56
61
  }
57
62
 
58
63
 
59
-
60
64
  // WRITE CALLBACK
61
65
  void Smalrubot::setupWrite(void (*writeCallback)(char *str)) {
62
66
  _writeCallback = writeCallback;
@@ -27,7 +27,7 @@ class Smalrubot {
27
27
  void parse(char c);
28
28
  void process();
29
29
 
30
- private:
30
+ protected:
31
31
  // Request storage.
32
32
  char request[8];
33
33
  int index;
@@ -46,6 +46,8 @@ class Smalrubot {
46
46
 
47
47
  Servo servos[12];
48
48
 
49
+ virtual void processCommand();
50
+
49
51
  // API-accessible functions.
50
52
  void setMode ();
51
53
  void dWrite ();
@@ -0,0 +1,183 @@
1
+ #include "SrStuduino.h"
2
+
3
+ typedef Smalrubot base;
4
+
5
+ SrStuduino::SrStuduino() {
6
+ }
7
+
8
+ /*
9
+ * 20-39: Initialize
10
+ * 40-59: Actuatuator
11
+ * 60-79: Sensor
12
+ **/
13
+ void SrStuduino::processCommand() {
14
+ switch(cmd) {
15
+ case 20:
16
+ setDcMotorCalibration();
17
+ break;
18
+ case 22:
19
+ initDcMotorPort();
20
+ break;
21
+ case 25:
22
+ initSensorPort();
23
+ break;
24
+
25
+ case 42:
26
+ dcMotorPower();
27
+ break;
28
+ case 43:
29
+ dcMotorControl();
30
+ break;
31
+
32
+ case 61:
33
+ getTouchSensorValue();
34
+ break;
35
+ case 62:
36
+ getLightSensorValue();
37
+ break;
38
+ case 64:
39
+ getIrPhotoreflectorValue();
40
+ break;
41
+
42
+ default:
43
+ base::processCommand();
44
+ break;
45
+ }
46
+ }
47
+
48
+ byte motorPorts[] = {
49
+ PORT_M1,
50
+ PORT_M2
51
+ };
52
+ byte analogPorts[] = {
53
+ PORT_A0,
54
+ PORT_A1,
55
+ PORT_A2,
56
+ PORT_A3,
57
+ PORT_A4,
58
+ PORT_A5,
59
+ PORT_A6,
60
+ PORT_A7
61
+ };
62
+ byte pids[] = {
63
+ PIDOPEN,
64
+ PIDLED,
65
+ PIDBUZZER,
66
+ PIDLIGHTSENSOR,
67
+ PIDSOUNDSENSOR,
68
+ PIDIRPHOTOREFLECTOR,
69
+ PIDACCELEROMETER,
70
+ PIDTOUCHSENSOR,
71
+ PIDPUSHSWITCH
72
+ };
73
+
74
+ /**
75
+ * !20<motor_index><rate>.
76
+ *
77
+ * motor_index(2): 0-1 => PORT_M1-PORT_M2
78
+ * rate(3): 0-100
79
+ **/
80
+ void SrStuduino::setDcMotorCalibration() {
81
+ byte calib[] = { 100, 100 };
82
+
83
+ calib[pin] = val;
84
+ studuino.SetDCMotorCalibration(calib);
85
+ }
86
+
87
+ /**
88
+ * !22<motor_index>.
89
+ *
90
+ * motor_index(2): 0-1 => PORT_M1-PORT_M2
91
+ **/
92
+ void SrStuduino::initDcMotorPort() {
93
+ studuino.InitDCMotorPort(motorPorts[pin]);
94
+ }
95
+
96
+ /**
97
+ * !25<sensor_index><pid_index>.
98
+ *
99
+ * sensor_index(2): 0-7 => PORT_A0-PORT_A7
100
+ * pid_index(3): 0 => PIDOPEN or
101
+ * 1 => PIDLED or
102
+ * 2 => PIDBUZZER or
103
+ * 3 => PIDLIGHTSENSOR or
104
+ * 4 => PIDSOUNDSENSOR or
105
+ * 5 => PIDRPHOTOREFLECTOR or
106
+ * 6 => PIDACCELEROMETER or
107
+ * 7 => PIDTOUCHSENSOR or
108
+ * 8 => PIDPUSHSWITCH
109
+ **/
110
+ void SrStuduino::initSensorPort() {
111
+ studuino.InitSensorPort(analogPorts[pin], pids[val]);
112
+ }
113
+
114
+ /**
115
+ * !42<motor_index><pace>.
116
+ *
117
+ * motor_index(2): 0-1 => PORT_M1-PORT_M2
118
+ * pace(3): 0-255
119
+ **/
120
+ void SrStuduino::dcMotorPower() {
121
+ studuino.DCMotorPower(motorPorts[pin], val);
122
+ }
123
+
124
+ /**
125
+ * !43<motor_index><rotation>.
126
+ *
127
+ * motor_index(2): 0-1 => PORT_M1-PORT_M2
128
+ * rotation_index(3): 0 => NORMAL or
129
+ * 1 => REVERSE or
130
+ * 2 => BRAKE or
131
+ * 3 => COAST
132
+ **/
133
+ void SrStuduino::dcMotorControl() {
134
+ byte rotations[] = { NORMAL, REVERSE, BRAKE, COAST };
135
+
136
+ studuino.DCMotorControl(motorPorts[pin], rotations[val]);
137
+ }
138
+
139
+ /**
140
+ * !61<sensor_index>.
141
+ *
142
+ * sensor_index(2): 0-7 => PORT_A0-PORT_A7
143
+ *
144
+ * return: <sensor_index><res>
145
+ *
146
+ * res: 0 => pressed or
147
+ * 1 => released
148
+ **/
149
+ void SrStuduino::getTouchSensorValue() {
150
+ byte res = studuino.GetTouchSensorValue(analogPorts[pin]);
151
+
152
+ sprintf(response, "%02d:%01d", pin, res);
153
+ }
154
+
155
+ /**
156
+ * !62<sensor_index>.
157
+ *
158
+ * sensor_index(2): 0-7 => PORT_A0-PORT_A7
159
+ *
160
+ * return: <sensor_index><res>
161
+ *
162
+ * res: 0-1023
163
+ **/
164
+ void SrStuduino::getLightSensorValue() {
165
+ int res = studuino.GetLightSensorValue(analogPorts[pin]);
166
+
167
+ sprintf(response, "%02d:%04d", pin, res);
168
+ }
169
+
170
+ /**
171
+ * !64<sensor_index>.
172
+ *
173
+ * sensor_index(2): 0-7 => PORT_A0-PORT_A7
174
+ *
175
+ * return: <sensor_index><res>
176
+ *
177
+ * res: 0-1023
178
+ **/
179
+ void SrStuduino::getIrPhotoreflectorValue() {
180
+ int res = studuino.GetIRPhotoreflectorValue(analogPorts[pin]);
181
+
182
+ sprintf(response, "%02d:%04d", pin, res);
183
+ }
@@ -0,0 +1,31 @@
1
+ /*
2
+ Library for smalrubot ruby gem.
3
+ */
4
+
5
+ #ifndef SrStuduino_h
6
+ #define SrStuduino_h
7
+
8
+ #include "Smalrubot.h"
9
+ #include "Studuino.h"
10
+
11
+ class SrStuduino : public Smalrubot {
12
+ public:
13
+ SrStuduino();
14
+
15
+ protected:
16
+ Studuino studuino;
17
+
18
+ virtual void processCommand();
19
+
20
+ void setDcMotorCalibration();
21
+ void initDcMotorPort();
22
+ void initSensorPort();
23
+ void dcMotorPower();
24
+ void dcMotorControl();
25
+ void getTouchSensorValue();
26
+ void getLightSensorValue();
27
+ void getIrPhotoreflectorValue();
28
+ };
29
+
30
+
31
+ #endif
data/sketch/sr/sr.ino CHANGED
@@ -7,7 +7,7 @@ void writeResponse(char *response) { Serial.println(response); }
7
7
  void (*writeCallback)(char *str) = writeResponse;
8
8
 
9
9
  void setup() {
10
- Serial.begin(115200);
10
+ Serial.begin(19200);
11
11
  smalrubot.setupWrite(writeCallback);
12
12
  }
13
13
 
@@ -0,0 +1,22 @@
1
+ #include "SrStuduino.h"
2
+ #include <Servo.h>
3
+ #include <Wire.h>
4
+ #include <MMA8653.h>
5
+ #include "Studuino.h"
6
+
7
+ SrStuduino smalrubot;
8
+
9
+ // Smalrubot.h doesn't handle TXRX. Setup a function to tell it to write to Serial.
10
+ void writeResponse(char *response) { Serial.println(response); }
11
+ void (*writeCallback)(char *str) = writeResponse;
12
+
13
+ void setup() {
14
+ Serial.begin(19200);
15
+ smalrubot.setupWrite(writeCallback);
16
+ }
17
+
18
+ void loop() {
19
+ while(Serial.available() > 0) smalrubot.parse(Serial.read());
20
+ Serial.flush();
21
+ }
22
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smalrubot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouji Takao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-24 00:00:00.000000000 Z
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyserial
@@ -85,6 +85,7 @@ files:
85
85
  - examples/test.rb
86
86
  - lib/smalrubot.rb
87
87
  - lib/smalrubot/board.rb
88
+ - lib/smalrubot/board/studuino.rb
88
89
  - lib/smalrubot/board_not_found.rb
89
90
  - lib/smalrubot/components.rb
90
91
  - lib/smalrubot/components/base_component.rb
@@ -96,7 +97,10 @@ files:
96
97
  - lib/smalrubot/version.rb
97
98
  - sketch/lib/Smalrubot.cpp
98
99
  - sketch/lib/Smalrubot.h
100
+ - sketch/lib/SrStuduino.cpp
101
+ - sketch/lib/SrStuduino.h
99
102
  - sketch/sr/sr.ino
103
+ - sketch/sr_studu/sr_studu.ino
100
104
  - smalrubot.gemspec
101
105
  - spec/lib/board_not_found_spec.rb
102
106
  - spec/lib/board_spec.rb