This will generate the message for the programme

Dependencies:   MODSERIAL Nanopb

Committer:
omatthews
Date:
Tue Aug 27 15:08:49 2019 +0000
Revision:
1:fa8f59ecc52a
Parent:
0:388a2a9f5247
Child:
2:b46d53a5f931
Latest parameters

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