This will generate the message for the programme

Dependencies:   MODSERIAL Nanopb

Committer:
omatthews
Date:
Tue Aug 27 09:24:17 2019 +0000
Revision:
0:388a2a9f5247
Child:
1:fa8f59ecc52a
For Paul

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omatthews 0:388a2a9f5247 1 #include "mbed.h"
omatthews 0:388a2a9f5247 2 #include "pb.h"
omatthews 0:388a2a9f5247 3 #include "pb_decode.h"
omatthews 0:388a2a9f5247 4 #include "pb_encode.h"
omatthews 0:388a2a9f5247 5 #include "memspcr.pb.h"
omatthews 0:388a2a9f5247 6 #include "MODSERIAL.h"
omatthews 0:388a2a9f5247 7 #include <vector>
omatthews 0:388a2a9f5247 8 #include <iterator>
omatthews 0:388a2a9f5247 9
omatthews 0:388a2a9f5247 10 DigitalOut led1(LED1);
omatthews 0:388a2a9f5247 11 MODSERIAL pc(USBTX, USBRX, 512);
omatthews 0:388a2a9f5247 12
omatthews 0:388a2a9f5247 13 uint8_t buffer[512];
omatthews 0:388a2a9f5247 14 size_t message_length;
omatthews 0:388a2a9f5247 15 bool status;
omatthews 0:388a2a9f5247 16 int i = 0;
omatthews 0:388a2a9f5247 17 unsigned int c;
omatthews 0:388a2a9f5247 18 memspcr_ExperimentConfiguration exp_config = memspcr_ExperimentConfiguration_init_zero;
omatthews 0:388a2a9f5247 19 int buffer_length;
omatthews 0:388a2a9f5247 20 int n_points;
omatthews 0:388a2a9f5247 21
omatthews 0:388a2a9f5247 22 std::vector<memspcr_ThermalStep> profile;
omatthews 0:388a2a9f5247 23
omatthews 0:388a2a9f5247 24
omatthews 0:388a2a9f5247 25 //Parameters
omatthews 0:388a2a9f5247 26 float resistance[5] = {0.4,0.5,0.4,0.7,0.5};
omatthews 0:388a2a9f5247 27 int elapsed_time_ms[5] = {500,1000,1500,2500,3000};
omatthews 0:388a2a9f5247 28 int camera_offset_ms[5] = {50,250,0,100,100};
omatthews 0:388a2a9f5247 29
omatthews 0:388a2a9f5247 30
omatthews 0:388a2a9f5247 31 void write_message()
omatthews 0:388a2a9f5247 32 {
omatthews 0:388a2a9f5247 33 pc.printf("%d ",message_length);
omatthews 0:388a2a9f5247 34 wait_ms(20);
omatthews 0:388a2a9f5247 35 for (int i = 0; i < message_length; i++)
omatthews 0:388a2a9f5247 36 {
omatthews 0:388a2a9f5247 37 pc.printf("%02X ",buffer[i]);
omatthews 0:388a2a9f5247 38 }
omatthews 0:388a2a9f5247 39 }
omatthews 0:388a2a9f5247 40
omatthews 0:388a2a9f5247 41
omatthews 0:388a2a9f5247 42 void encode_message()
omatthews 0:388a2a9f5247 43 {
omatthews 0:388a2a9f5247 44 pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
omatthews 0:388a2a9f5247 45 status = pb_encode(&ostream, memspcr_ExperimentConfiguration_fields, &exp_config);
omatthews 0:388a2a9f5247 46 message_length = ostream.bytes_written;
omatthews 0:388a2a9f5247 47
omatthews 0:388a2a9f5247 48 if (!status)
omatthews 0:388a2a9f5247 49 {
omatthews 0:388a2a9f5247 50 pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream));
omatthews 0:388a2a9f5247 51 }
omatthews 0:388a2a9f5247 52 }
omatthews 0:388a2a9f5247 53
omatthews 0:388a2a9f5247 54 bool encode_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
omatthews 0:388a2a9f5247 55 {
omatthews 0:388a2a9f5247 56 vector <memspcr_ThermalStep> * elements = (vector <memspcr_ThermalStep> *)(*arg);
omatthews 0:388a2a9f5247 57 vector <memspcr_ThermalStep>::iterator it;
omatthews 0:388a2a9f5247 58
omatthews 0:388a2a9f5247 59 for (it = elements->begin(); it < elements->end(); it ++)
omatthews 0:388a2a9f5247 60 {
omatthews 0:388a2a9f5247 61 if (!pb_encode_tag_for_field(stream, field))
omatthews 0:388a2a9f5247 62 {
omatthews 0:388a2a9f5247 63 printf("Encode callback error: %s\n", PB_GET_ERROR(stream));
omatthews 0:388a2a9f5247 64 return false;
omatthews 0:388a2a9f5247 65 }
omatthews 0:388a2a9f5247 66 if(!pb_encode_submessage(stream, memspcr_ThermalStep_fields, &(*it)))
omatthews 0:388a2a9f5247 67 return false;
omatthews 0:388a2a9f5247 68 }
omatthews 0:388a2a9f5247 69 return true;
omatthews 0:388a2a9f5247 70 }
omatthews 0:388a2a9f5247 71
omatthews 0:388a2a9f5247 72
omatthews 0:388a2a9f5247 73 int main()
omatthews 0:388a2a9f5247 74 {
omatthews 0:388a2a9f5247 75 /* This is the buffer where we will store our message. */
omatthews 0:388a2a9f5247 76 pc.baud(115200);
omatthews 0:388a2a9f5247 77 n_points = sizeof(resistance)/sizeof(float);
omatthews 0:388a2a9f5247 78 buffer_length = sizeof(buffer)/sizeof(uint8_t);
omatthews 0:388a2a9f5247 79
omatthews 0:388a2a9f5247 80 //Prepare message
omatthews 0:388a2a9f5247 81 for (i = 0; i < n_points; i++)
omatthews 0:388a2a9f5247 82 {
omatthews 0:388a2a9f5247 83 memspcr_ThermalStep step = memspcr_ThermalStep_init_zero;
omatthews 0:388a2a9f5247 84 step.resistance = resistance[i];
omatthews 0:388a2a9f5247 85 step.elapsed_time_ms = elapsed_time_ms[i];
omatthews 0:388a2a9f5247 86 step.camera_offset_ms = camera_offset_ms[i];
omatthews 0:388a2a9f5247 87 profile.push_back(step);
omatthews 0:388a2a9f5247 88 }
omatthews 0:388a2a9f5247 89 //Set set points
omatthews 0:388a2a9f5247 90 exp_config.thermal.profile.arg = &profile;
omatthews 0:388a2a9f5247 91 exp_config.thermal.profile.funcs.encode = encode_callback;
omatthews 0:388a2a9f5247 92 exp_config.thermal.guard_drive_ratio = 0.26;
omatthews 0:388a2a9f5247 93 exp_config.thermal.selected_heater = memspcr_ThermalConfiguration_Heater_MAIN;
omatthews 0:388a2a9f5247 94 exp_config.thermal.adc_settling_time_us = 60;
omatthews 0:388a2a9f5247 95 exp_config.thermal.pid_kp = 6.0;
omatthews 0:388a2a9f5247 96 exp_config.thermal.pid_integral_time = 0.5;
omatthews 0:388a2a9f5247 97 exp_config.thermal.control_loop_interval = 3000;
omatthews 0:388a2a9f5247 98 exp_config.optics.pre_trigger_ms = 1;
omatthews 0:388a2a9f5247 99 exp_config.optics.on_time_ms = 1;
omatthews 0:388a2a9f5247 100 exp_config.optics.led_pwm = 0.5;
omatthews 0:388a2a9f5247 101 exp_config.fluidics.pressure_setpoint = 0.4;
omatthews 0:388a2a9f5247 102 exp_config.fluidics.pressure_hysterisis = 0.01;
omatthews 0:388a2a9f5247 103 exp_config.logging_interval_ms = 50;
omatthews 0:388a2a9f5247 104
omatthews 0:388a2a9f5247 105
omatthews 0:388a2a9f5247 106 encode_message();
omatthews 0:388a2a9f5247 107 write_message();
omatthews 0:388a2a9f5247 108
omatthews 0:388a2a9f5247 109 return 0;
omatthews 0:388a2a9f5247 110 }