process_command.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016-17, Universidad de Almeria
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 
36 /*Beginning of Auto generated code by Atmel studio */
37 #include <Arduino.h>
38 /*End of auto generated code by Atmel studio */
39 
41 
42 #include "mod_dac_max5500.h"
43 #include "Encoder_EMS22A.h"
44 
45 // Originally designed for atmega328P.
46 // --------------------------------------
47 
48 #include <Wire.h>
49 #include <SPI.h>
50 
51 //Beginning of Auto generated function prototypes by Atmel Studio
52 //End of Auto generated function prototypes by Atmel Studio
53 
54 // Fixed pins configuration for this hardware:
55 #include "config.h"
56 
58 {
59  unsigned long TIMEOUT_TICKS;
60 
61  bool PWM_any;
62  unsigned long PWM_last_changed[16];
63 
64  bool DAC_any;
65  unsigned long DAC_last_changed[4];
66 
68  TIMEOUT_TICKS(1000),
69  PWM_any(false),
70  DAC_any(false)
71  {
72  ::memset(PWM_last_changed,0, sizeof(PWM_last_changed));
73  ::memset(DAC_last_changed,0, sizeof(DAC_last_changed));
74  }
75 };
76 
78 
79 
80 void flash_led(int ntimes, int nms)
81 {
83  for (int i=0;i<ntimes;i++)
84  {
86  digitalWrite(PIN_LED, LOW); delay(nms);
87  }
88 }
89 
90 void send_simple_opcode_frame(const uint8_t op)
91 {
92  const uint8_t rx[] = { FRAME_START_FLAG, op, 0x00, 0x00, FRAME_END_FLAG };
93  Serial.write(rx,sizeof(rx));
94 }
95 
96 void process_command(const uint8_t opcode, const uint8_t datalen, const uint8_t*data)
97 {
98  switch (opcode)
99  {
100  case OP_NOP:
101  {
103 
104  // No-operation: just a fake command to check if comms are alive
106  }
107  break;
108 
109  case OP_SET_DAC:
110  {
112 
114  memcpy(&dac_req,data, sizeof(dac_req));
115 
116  // Init upon first usage:
117  static bool dac_init = false;
118  if (!dac_init)
119  {
121  dac_init = true;
122  }
123  const uint16_t dac_value = (uint16_t(dac_req.dac_value_HI) << 8) | dac_req.dac_value_LO;
124  mod_dac_max5500_update_single_DAC(dac_req.dac_index,dac_value);
125 
126  if (dac_req.flag_enable_timeout)
127  {
128  if (dac_req.dac_index<sizeof(PendingTimeouts.DAC_last_changed)/sizeof(PendingTimeouts.DAC_last_changed[0]))
129  {
130  PendingTimeouts.DAC_any=true;
131  PendingTimeouts.DAC_last_changed[dac_req.dac_index] = millis();
132  }
133  }
134 
135  // send answer back:
137  }
138  break;
139  case OP_SET_GPIO:
140  {
142 
143  const uint8_t pin_no = data[0];
144  const uint8_t pin_val = data[1];
145  pinMode(pin_no, OUTPUT);
146  digitalWrite(pin_no, pin_val);
147 
148  // send answer back:
150  }
151  break;
152  case OP_GET_GPIO:
153  {
155 
156  const uint8_t pin_no = data[0];
157  pinMode(pin_no, INPUT);
158  const uint8_t val = digitalRead(pin_no);
159 
160  // send answer back:
161  const uint8_t rx[] = { FRAME_START_FLAG, RESP_GET_GPIO, 0x01, pin_no, val, 0x00 +pin_no+ val/*checksum*/, FRAME_END_FLAG };
162  Serial.write(rx,sizeof(rx));
163  }
164  break;
165 
166  case OP_START_CONT_ADC:
167  {
169 
171  memcpy(&adc_req,data, sizeof(adc_req));
172 
173  // Setup vars for ADC task:
175  for (int i=0;i<8;i++) {
176  ADC_active_channels[i] = 0;
177  if (adc_req.active_channels[i]>=0) {
178  ADC_active_channels[i] = adc_req.active_channels[i];
180  }
181  }
183 
184  // Enable ADC with internal/default reference:
186 
187  // send answer back:
189  }
190  break;
191 
192  case OP_STOP_CONT_ADC:
193  {
195 
197 
198  // send answer back:
200  }
201  break;
202 
203  case OP_SET_PWM:
204  {
206 
208  memcpy(&pwm_req,data, sizeof(pwm_req));
209 
210  pinMode(pwm_req.pin_index, OUTPUT);
211  analogWrite(pwm_req.pin_index, pwm_req.analog_value);
212 
213  if (pwm_req.flag_enable_timeout)
214  {
215  if (pwm_req.pin_index<sizeof(PendingTimeouts.PWM_last_changed)/sizeof(PendingTimeouts.PWM_last_changed[0]))
216  {
217  PendingTimeouts.PWM_any=true;
218  PendingTimeouts.PWM_last_changed[pwm_req.pin_index] = millis();
219  }
220  }
221 
222  // send answer back:
224  }
225  break;
226 
227  case OP_START_ENCODERS:
228  {
230 
232  memcpy(&enc_req,data, sizeof(enc_req));
233 
234  init_encoders(enc_req);
235  ENCODERS_active=true;
236 
237  // send answer back:
239  }
240  break;
241 
242  case OP_STOP_ENCODERS:
243  {
245  init_encoders(cmd_empty);
246  ENCODERS_active=false;
247 
248  // send answer back:
250  }
251  break;
252 
253  case OP_START_EMS22A:
254  {
256 
258  memcpy(&EMS22A_req,data, sizeof(EMS22A_req));
259  if (init_EMS22A(
260  EMS22A_req.ENCODER_ABS_CS,EMS22A_req.ENCODER_ABS_CLK,
261  EMS22A_req.ENCODER_ABS_DO, EMS22A_req.sampling_period_ms
262  ))
263  {
264 
265  EMS22A_active = true;
266  // send answer back:
268  }
269  else
270  {
271  // params error:
273  }
274  }
275  break;
276 
277  case OP_STOP_EMS22A:
278  {
279  EMS22A_active = false;
280  // send answer back:
282  }
283  break;
284 
285  default:
286  {
287  // Error:
289  }
290  break;
291  };
292 }
293 
295 {
296  TimeoutData &pt = PendingTimeouts; // shortcut
297  const unsigned long tnow = millis();
298 
299  if (pt.DAC_any)
300  {
301  pt.DAC_any=false; // if no timeout is set, don't waste time in the next time we are called.
302  for (uint8_t i=0;i<sizeof(pt.DAC_last_changed)/sizeof(pt.DAC_last_changed[0]);i++)
303  {
304  if (pt.DAC_last_changed[i]!=0)
305  {
306  pt.DAC_any=true;
307  if (tnow - pt.DAC_last_changed[i] > pt.TIMEOUT_TICKS)
308  {
309  // Watchdog timer event!
310  pt.DAC_last_changed[i]=0; // reset this one
312  }
313 
314  }
315  }
316  }
317 
318  if (pt.PWM_any)
319  {
320  pt.PWM_any=false; // if no timeout is set, don't waste time in the next time we are called.
321  for (uint8_t i=0;i<sizeof(pt.PWM_last_changed)/sizeof(pt.PWM_last_changed[0]);i++)
322  {
323  if (pt.PWM_last_changed[i]!=0)
324  {
325  pt.PWM_any=true;
326  if (tnow - pt.PWM_last_changed[i] > pt.TIMEOUT_TICKS)
327  {
328  // Watchdog timer event!
329  pt.PWM_last_changed[i]=0; // reset this one
330  analogWrite(i, 0);
331  }
332  }
333  }
334  }
335 }
336 
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
void mod_dac_max5500_init()
unsigned long PWM_last_changed[16]
Last timestamp (millis()) for each PWM channel.
void pinMode(uint8_t, uint8_t)
void init_encoders(const TFrameCMD_ENCODERS_start_payload_t &cmd)
unsigned long DAC_last_changed[4]
Last timestamp (millis()) for each DAC channel.
uint8_t num_active_ADC_channels
uint8_t analog_value
0-255 maps to 0% to 100% duty cycle
bool EMS22A_active
uint16_t ADC_sampling_period_ms
void analogWrite(uint8_t, int)
#define OUTPUT
Definition: Arduino.h:44
#define HIGH
Definition: Arduino.h:40
void analogReference(uint8_t mode)
Definition: wiring_analog.c:30
#define INPUT
Definition: Arduino.h:43
void send_simple_opcode_frame(const uint8_t op)
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
#define FRAME_START_FLAG
void delay(unsigned long)
Definition: wiring.c:106
unsigned long millis(void)
Definition: wiring.c:65
bool init_EMS22A(int8_t ENCODER_ABS_CS, int8_t ENCODER_ABS_CLK, int8_t ENCODER_ABS_DO, uint16_t sampling_period_ms)
uint8_t ADC_active_channels[MAX_ADC_CHANNELS]
int digitalRead(uint8_t)
void process_command(const uint8_t opcode, const uint8_t datalen, const uint8_t *data)
unsigned long TIMEOUT_TICKS
Number of millis() ticks to timeout an output signal. Default=1000 ms.
void digitalWrite(uint8_t, uint8_t)
#define DEFAULT
Definition: Arduino.h:83
#define LOW
Definition: Arduino.h:41
uint8_t use_internal_refvolt
0 or 1. Default=0
GLuint GLfloat * val
#define INTERNAL
Definition: Arduino.h:81
TimeoutData PendingTimeouts
void flash_led(int ntimes, int nms)
bool ENCODERS_active
void process_timeouts()
uint16_t measure_period_ms
Default = 200.
#define FRAME_END_FLAG
const int PIN_LED
Definition: config.h:40
void mod_dac_max5500_update_single_DAC(uint8_t dac_idx, uint16_t dac_value)


arduino_daq
Author(s):
autogenerated on Mon Jun 10 2019 12:46:03